i**p 发帖数: 902 | 1 char s1[] = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
It is Ok. Both s1 and s3 will be "Hello world".
char *s1 = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
It is runtime error. Can you explain? |
|
P********e 发帖数: 2610 | 2 strcat(char*, const char*)
s1必须是char *;
char *s1 = "hello";
s1是指向 const char*的,因为s1是desti |
|
i**p 发帖数: 902 | 3 How does it work?
char s1[] = "Hello ";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
Both s1 and s3 will be "Hello world". |
|
c*****t 发帖数: 1879 | 4 Just forget using strcat function. sprintf and strcpy are
less error prone. |
|
O*******d 发帖数: 20343 | 5 如果你把s1和s2中间插一个,看看能不能运行。再看一看_s1变成了什么。
char s1[] = "Hello ";
char _s1[] = "whatever";
char s2[] = "world";
char *s3;
s3 = strcat(s1, s2);
printf("s1 %s _s1 %s\n", s1, _s1); |
|
i**p 发帖数: 902 | 6 Of course, s1[] and *s1 are different. I have seen lots of programmer tried
to do something like “strcpy (s2, s1, sizeof(s1))” with s1 defined as *s1.
When is happened, I partly blame the programmer and mostly blame why C does
not do something to prevent this from the beginning or a latter improvement
. Anyway, this is a dangerous pitfall for a beginner and mostly to be an
interview question.
For the strcat(), it is much easier to generate a compiler warning or
runtime error for s1[] than *s1, b |
|
c**********e 发帖数: 2007 | 7 这个Strategy design pattern的例子为什么人为得弄得这么复杂?
#include
#include
#include
using namespace std;
class Strategy;
class TestBed
{
public:
enum StrategyType
{
Dummy, Left, Right, Center
};
TestBed()
{
strategy_ = NULL;
}
void setStrategy(int type, int width);
void doIt();
private:
Strategy *strategy_;
};
class Strategy
{
public:
Strategy(int width): width_(width){}
void format()
{
char line[80], wo... 阅读全帖 |
|
f**********t 发帖数: 1001 | 8 我想实现Card class里面的输入牌的名字
然后代码如下:
char* Card::NameOf() {
static char szName[20];
static char *Numbers[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "
10", "Jack", "Queen", "King"};
static char *Suits[] = {"Clubs", "Spades", "Hearts", "Diamonds"};
if (GetCardinal() < 13) {
strcat(szName, Numbers[GetCardinal()]);
}
strcat(szName, " of");
if (GetSuit() <= 4) {
strcat(szName, Suits[GetSuit() - 1]);
}
return szName;
}
reference在这里:http://msdn.micr... 阅读全帖 |
|
|
c*********n 发帖数: 1057 | 10 用recursive容易很多啊
void comb(int l,int r, char *result,int size){
//printf("%d, %d, %s\n",l,r,result);
if(l==0 && r==0){
printf("%s\n",result);
return;
}
if(l==r){
strcat(result,"(");
comb(l-1,r,result,size);
}
else if(l
if(l>0 && r>0){
char *result1=calloc(size,1);
strcpy(result1,result);
strcat(r |
|
l*********o 发帖数: 3091 | 11 #include
#include
int convert(char* src, char* dst)
{
*dst = 0;
int pos;
char str[64];
while(*src)
{
if(*src < '0' || *src > '9') return 1;
pos = 1;
while(*src==*(src+pos))pos++;
sprintf(str, "%d", pos);
strcat(dst, str);
dst += strlen(str);
sprintf(str, "%c", *src);
strcat(dst, str);
dst += strlen(str);
src += pos;
}
return 0;
}
int main()
{
int n = 29;
int... 阅读全帖 |
|
d***a 发帖数: 316 | 12 declare 一个 C++ string class object,
然后用 .c_str() 将其转换成一个 c string
再用 strcat() 加上一个后缀。
strcat(userinput, "_data_extracted.txt")
这个最后得到的 c string 有时会在原string和新加的后缀之间又加了一些东西,比如
%B 或者 %FF%FF。有时又不出现。请教为什么,如何处理?多谢。 |
|
l****q 发帖数: 177 | 13 先cft~其实最近job market有点变化,不一定是你的问题
人品差的进好公司的多了去了,不要想这些没有用的
下面说些可能对你有用的:
1。你第一个C程序的算法不够好,这个问题很常规,你在本版可以找到优化解法
2。你第二个C程序有很多问题:首先不要使用a,b这种数组名;任何变量包括数组要初
始化;
hashtable里面key到底是什么,c没有这种数组,就算是有,也得是字符串吧,结束符
也没有;
strcat在工业界多数用snprintf代替。。。。。。
你看,就算算法是对的,这个算法本身很简单,代码质量就成了关键。面试的人那么多
,怎么才能突
出自己。希望对你有帮助。继续努力,一定会越来越好的~~~
char * suggestion(char input[])
{
int len=strlen(input);
for(int i=0;i
{
char a[i];
char b[len-i];
for(int j=0;j
a[j]=input[j];
for(int j=i+1;j
b[j+i |
|
s******n 发帖数: 57 | 14 谢谢指点,第一个c++程序还有更优的算法么,我的算法是O(n),再优化是什么?请指教。
还有,第二个问题忘了个东西,抱歉,interviewer说所有词汇的在一个hashtable里已
经存在。
strcat和printf的区别要好好看看,还有变量名称,平时总是提醒自己,不知道怎么当
时就没注意这
个事情。谢谢了 |
|
p********7 发帖数: 549 | 15 哥们儿,感谢你发出这么详细的面经,还有下面各位的回答,让我感受颇深啊。
我还发现一个问题,对于第二个题。
strcat的时候数组a大小明显不够啊。 |
|
p********7 发帖数: 549 | 16 哥们儿,感谢你发出这么详细的面经,还有下面各位的回答,让我感受颇深啊。
我还发现一个问题,对于第二个题。
strcat的时候数组a大小明显不够啊。 |
|
y*c 发帖数: 904 | 17
strdup 就是需要用户free的,貌似是说不太好。要么就用户分配空间,像strcat。要
么就用stl的string。 |
|
l*****a 发帖数: 14598 | 18 strcpy
strcat
strcmp
strstr
substring |
|
w**7 发帖数: 71 | 19 上周面了Yahoo,LEAP team,开始是HM面,问了简单的问题,感觉HM是华人,起码我的
名字他能念对,后来他给我发信说,给我找了个人面试,我一查,那人也是国人,还是
交大的,估计没准还能有点优势。
结果面试那天,电话一接是个三哥……三哥也没啥,就是交流有点问题
题目:
1. 介绍Project
2. 编程,strcpy,但是我写了程序,发现他实际要的是strcat,那就先把dst的指针移
到末尾,然后做同样strcpy的事。后来说了程序,感觉他又挑了些问题,也不知道是我
自己理解有问题还是怎么,感觉不太顺,然后就move下一题了
2. 说说编程思想,在一段有序数组中找一个目标数。直接binary search,简单吧,连
程序都没让我写,让我说思路,后来问了如果没找到要插入怎么弄, 如果是链表如何
插入。
3. Linux系统命令,杀死进程,kill -l PID。在文件夹及其所有子文件夹里的文件中
搜索一个字符,grep yourinput -R dir/*
然后就结束了
面对三哥还是有点怵,不知道咋样,Move on 求bless.... |
|
t*******1 发帖数: 206 | 20 编译原理和计算机理论是计算机学科的基础。不知道是学的不够好所以觉得无聊还是因
为其他。这样说吧,编程语言对于计算机就相当于方程式的配平对于化学。cs的不仅仅
会编程,还应该知道为什么在程序中不能用strcmp,strcat这些函数,还应该知道什么
是递归,动态规划。 |
|
b*********n 发帖数: 1258 | 21 char* readline()
{
size_t pos=0;
char* first=readline4096();
char* term_pos=0;
char* out=first;
while(!term_pos)
{
term_pos=min(strchr(first,'\n'),strchr(first,'\0'));
if(term_pos)
{
pos+=term_pos-first;
return substr(out, pos);
}
strcat(out,first);
first=readline4096();
}
} |
|
m*****9 发帖数: 29 | 22 见了四个人,面经如下:
coding: given a piece of code, analyze the performance and security, strcat
string, 背包问题,100刀买三个动物问题。
概念:语言间difference,内存分配问题,线程,面向对象,设计模式,debug技巧
behavior:忠诚度问题,proud thing
brain teaser: train相遇问题 |
|
h****e 发帖数: 928 | 23 Bless!这些问题看起来都挺reasonable的。
strcat |
|
d****g 发帖数: 1049 | 24 借你的牛刀杀只小鸡:)
我正在写一个处理格式文本的程序。想用flex,感觉是用不到yacc.
文件格式很简单,就是:
关键词:数据
但是文本里有不同数据块,比如:
类型: 联系方式
姓名:xxx
地址:xxx
类型:产品
名称: xxx
型号: xxx
类型:运动
名称:xxx
类别:xxx
我觉得用flex就足够了,因为结构比较简单。
我看了看flex的说明,有一点不知道怎么弄。
就是我拿到关键词以后怎么再拿到数据?
我应该在下面的规则里面加一个什么规则?
我现在是用".", 然后把所有不和关键字匹配
的字符全送到一个字串,可是觉得这么做很慢,
而且好像也很笨:)
%%
{keyword} {printf("%s\n", yytext);}
. {strcat(data,yytext);}
%% |
|
Q**a 发帖数: 406 | 25 贴完整代码,这样说不清楚。看你的意思userinput就是.c_str()转换出来的const
char*,但strcat的第一个参数应该是char*,你中间干啥了?
以及,你要干的这件事情直接让那个string += "..."就行 |
|
d***a 发帖数: 316 | 26 想法是用户输入一个文件名,例如 someresults.txt
然后用ifstream读入,处理后,用ofstream保存为 someresults_data_extracted.txt
用户输入的文件后缀要去掉。
以下是产生问题的部分code,其它省略。
整个程序g++编译通过的。
#include
#include
#include
#include
cout << "Enter the file to work on: ";
string originName;
getline(cin, originName);
// code for ifstream to read a file
…..
char * tempOut = new char [originName.size()+16];
strncpy(tempOut, originName.c_str(), originName.size()-4);
ofstream fout;
fout.open( strcat(tempO... 阅读全帖 |
|
a***d 发帖数: 3 | 27 Code
strcpy(s, "SELECT * FROM ");
strcat(s, exp_name);
EXEC SQL PREPARE q FROM :s;
EXEC SQL EXECUTE q ;
Error
sql error Too few arguments in line 62.
不明白呀,有什么好办法把table的名字很好的放进query里吗?
用using的话带引号,很烦人
thanks in advance :) |
|
e******r 发帖数: 220 | 28 How should I write the code, if I want the program output the strings.
Thanks
"family0"
"family1"
"family2"
"family3"
.....
"family10"
.....
"family99"
"family100"
........
"family999"
"family1000"
........
"family1999"
I tried
for(int q=0; q<2000; q++)
strcat("family",(char*)&q)
but it didn't work |
|
R********n 发帖数: 3601 | 29 我在VS下面用
#include
就可以用strcmp() strcat()等等命令。难道不用include 么?还是说
iostream已经包括了? |
|
p**v 发帖数: 853 | 30 用strtok()把45和568定位出来,并得到各自字符长度,
找个stringify()将1.095换成string.c_str(),
再组合起来。
用char挺麻烦的,要用好几次strcpy,strcat,strlen |
|
i**p 发帖数: 902 | 31 Do you mean
char *s1 = "hello"; s1 points to a const string? |
|
|
d****2 发帖数: 6250 | 33 you totally mess up the memory. s1 must be allocated with enough space first
. |
|
i**p 发帖数: 902 | 34 How about
char s1[] = "hello";
I think the size of s1 has been set to 6. how does it work? |
|
d****2 发帖数: 6250 | 35
it does not work. any c programmer writes this way should be hanged. |
|
i**p 发帖数: 902 | 36 I run it by VC. It prints both s1 and s3 as "hello world". |
|
|
i**p 发帖数: 902 | 38 then what tool can help us to find this programming error? |
|
d****2 发帖数: 6250 | 39
that is a good research question. MSFT will be very interested to fix their
crappy code base. |
|
i**p 发帖数: 902 | 40 A good compiler should at least give a waring, but VC++ does not.
their |
|
d****2 发帖数: 6250 | 41
google buffer overflow. |
|
t****t 发帖数: 6806 | 42 name a compiler that will give a warning for this. |
|
m*****e 发帖数: 4193 | 43
It's not ok. You just got lucky. |
|
i**p 发帖数: 902 | 44 I know what all of you talk about. see my original post. My qustion is s1[]
and *s1 have different result, which means *s1 is more protective than s1[].
Why not make them the same?
char s1[] = "Hello ";
char *s1 = "Hello "; |
|
d****2 发帖数: 6250 | 45 char [] has space allocated on stack
char * is a pointer to static text area, write protected.
BTW, char [] and char * are different thing. Compiler does some translation
for you so you won't notice the difference most of time. Try sizeof(s1) in
both cases and you will know why. |
|
t****t 发帖数: 6806 | 46 在你指责一个语言有这样那样的问题的时候, 有必要考虑语言设计时的历史环境. 为了
保持高速度, C语言里本来就没有数组边界检查, 更何况数组在函数调用里自动退化为指
针,数组尺寸信息早就没有了.
嫌C语言不安全的话,有很多更安全的语言可以用.就算是C++, 也有string class.
不过, 会在strcpy()里写上三个参数的, 可以想见你连门都没入. (FYI, 有三个参数的
那个叫做strncpy().) 门都没入的人还是虚心一点的好, 不要连纸上谈兵都谈不来,被人
笑掉大牙.
tried
s1.
does
improvement
an |
|
d****2 发帖数: 6250 | 47 funny, OP is asking beginner questions yet acts like a pro wannabe. no wonder such forums are boring. compiler is harder than you think. |
|
i**p 发帖数: 902 | 48 小屁孩拣了个大包子,乐得找不到了北。老哥虽民工出身,码砖的事早就不干了。听说
过大博士不知道 10000000是负几吗?没听说过只能说你太嫩。我到是在民工
学徒其间老听到同道笑话师傅打字没他快,你也就和他有一比了。
为指
被人 |
|
m*****e 发帖数: 4193 | 49 Please take your troll somewhere else. |
|
t****t 发帖数: 6806 | 50 虽然不知道你说的是啥,不过常来的都知道,我不是靠写程序吃饭的 |
|