d********i 发帖数: 8 | 1 一个字符串, 比如说:"mitbbs is a very nice place", 求写一个FUNCTION, 把字符串
里面的单词REVERSE,就是说,上述字符串为PARAMETER, FUNCTION 输出应该为"place
nice very a is mitbbs".
请你们写出FUNCTION IN C. PARAMETER可以按照自己情况定. |
c********e 发帖数: 383 | 2 two pass
1st pass reverse the whole string
2nd reverse each word
will work and the coding part is easy.
【在 d********i 的大作中提到】 : 一个字符串, 比如说:"mitbbs is a very nice place", 求写一个FUNCTION, 把字符串 : 里面的单词REVERSE,就是说,上述字符串为PARAMETER, FUNCTION 输出应该为"place : nice very a is mitbbs". : 请你们写出FUNCTION IN C. PARAMETER可以按照自己情况定.
|
p*u 发帖数: 2454 | 3 有微软那年就有这道破题了,查查精华区吧。
【在 c********e 的大作中提到】 : two pass : 1st pass reverse the whole string : 2nd reverse each word : will work and the coding part is easy.
|
t****t 发帖数: 6806 | 4 这样的题也好意思贴出来
【在 p*u 的大作中提到】 : 有微软那年就有这道破题了,查查精华区吧。
|
k****n 发帖数: 1334 | 5 是不是就是面微软?
【在 d********i 的大作中提到】 : 一个字符串, 比如说:"mitbbs is a very nice place", 求写一个FUNCTION, 把字符串 : 里面的单词REVERSE,就是说,上述字符串为PARAMETER, FUNCTION 输出应该为"place : nice very a is mitbbs". : 请你们写出FUNCTION IN C. PARAMETER可以按照自己情况定.
|
O*******d 发帖数: 20343 | 6 我几个地方面试都有这道题,成经典考题了。 建议大家把答案牢牢记住。 |
O*******d 发帖数: 20343 | 7 把答案贴在下边
void reverseString(char *st)
{
int len;
char *ptr1, *ptr2, *ptr3;
char c;
len = strlen(st);
ptr1 = st;
ptr2 = st + len - 1;
/* first pass. Reverse entire string */
while(ptr2 > ptr1)
{
c = *ptr1;
*ptr1 = *ptr2;
*ptr2 = c;
ptr1++;
ptr2--;
}
/* second pass. Reverse each word */
ptr1 = ptr3 = st;
while(ptr1 < (st + len))
{
while(ptr3 < (st + len) && *ptr3 != ' ')
ptr3++;
|
i*c 发帖数: 1132 | 8 我试了一下code,为什么第一个正常, 第二个会出错?
1:
int main(){
char ch[]="abc def ghi jkl mni";
reverseString(ch)
return 0;
}
2:
int main(){
char *ch="abc def ghi jkl mni";
reverseString(ch)
return 0;
}
【在 O*******d 的大作中提到】 : 把答案贴在下边 : void reverseString(char *st) : { : int len; : char *ptr1, *ptr2, *ptr3; : char c; : len = strlen(st); : ptr1 = st; : ptr2 = st + len - 1; : /* first pass. Reverse entire string */
|
c**t 发帖数: 2744 | 9 月经题
【在 i*c 的大作中提到】 : 我试了一下code,为什么第一个正常, 第二个会出错? : 1: : int main(){ : char ch[]="abc def ghi jkl mni"; : reverseString(ch) : return 0; : } : 2: : int main(){ : char *ch="abc def ghi jkl mni";
|
i*c 发帖数: 1132 | 10 我是新手,能否指点一下? 多谢。
【在 c**t 的大作中提到】 : 月经题
|
|
|
t****t 发帖数: 6806 | |
i*c 发帖数: 1132 | |
O*******d 发帖数: 20343 | 13 Do you notice that the char pointer passed to the function is not a constant
. The content it points to has changed after the function call.
char ch[]="abc def ghi jkl mni"; allow the string to be changed because ch
has its own memory.
char *ch="abc def ghi jkl mni"; does not allow the string to be changed
because "abc def ghi jkl mni" is a constant and ch points to it.
【在 i*c 的大作中提到】 : 我试了一下code,为什么第一个正常, 第二个会出错? : 1: : int main(){ : char ch[]="abc def ghi jkl mni"; : reverseString(ch) : return 0; : } : 2: : int main(){ : char *ch="abc def ghi jkl mni";
|
i*c 发帖数: 1132 | 14 Yes, I got the point. I just solved two similar questions for an online C
test
tonight.
constant
【在 O*******d 的大作中提到】 : Do you notice that the char pointer passed to the function is not a constant : . The content it points to has changed after the function call. : char ch[]="abc def ghi jkl mni"; allow the string to be changed because ch : has its own memory. : char *ch="abc def ghi jkl mni"; does not allow the string to be changed : because "abc def ghi jkl mni" is a constant and ch points to it.
|
s*******k 发帖数: 252 | 15 i have a less mind-bending solution
just start from the end and copy each word whenever you encounter a blank
【在 c********e 的大作中提到】 : two pass : 1st pass reverse the whole string : 2nd reverse each word : will work and the coding part is easy.
|
t****t 发帖数: 6806 | 16 which requires another buffer. the previous solution is in-place.
【在 s*******k 的大作中提到】 : i have a less mind-bending solution : just start from the end and copy each word whenever you encounter a blank
|
O*******d 发帖数: 20343 | 17 Usually, the interviewers ask you to use as less memory as possible. Your
solution requires another buffer.
【在 s*******k 的大作中提到】 : i have a less mind-bending solution : just start from the end and copy each word whenever you encounter a blank
|