由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个面试题目,用C实现
相关主题
关于二维矩阵的C的问题请教一个关于字符指针的简单问题
typedef的一个问题difference between: char** p and char*p[] ??
讨论 找单链表倒数m的节点 (转载)address of an array
四道C++面试题问一个C++函数Parameter的问题
C ++ 问题A question about c++ pointer
reverse words, not the Microsoft one!!!问个字符串的基本问题
Default function template arguments这段代码为何要这样做?
ajax小问题a simple C++ question
相关话题的讨论汇总
话题: ptr1话题: ptr2话题: function话题: ptr3话题: char
进入Programming版参与讨论
1 (共1页)
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 的大作中提到】
: 月经题
相关主题
reverse words, not the Microsoft one!!!请教一个关于字符指针的简单问题
Default function template argumentsdifference between: char** p and char*p[] ??
ajax小问题address of an array
进入Programming版参与讨论
t****t
发帖数: 6806
11
http://c-faq.com/charstring/strlitinit.html

【在 i*c 的大作中提到】
: 我是新手,能否指点一下? 多谢。
i*c
发帖数: 1132
12
got it, thanks.

【在 t****t 的大作中提到】
: http://c-faq.com/charstring/strlitinit.html
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

1 (共1页)
进入Programming版参与讨论
相关主题
a simple C++ questionC ++ 问题
初学C,对什么该free一直搞不明白reverse words, not the Microsoft one!!!
如何动态分配内存来存储输入的不定长的字符串,char not string类型的Default function template arguments
内存管理的问题ajax小问题
关于二维矩阵的C的问题请教一个关于字符指针的简单问题
typedef的一个问题difference between: char** p and char*p[] ??
讨论 找单链表倒数m的节点 (转载)address of an array
四道C++面试题问一个C++函数Parameter的问题
相关话题的讨论汇总
话题: ptr1话题: ptr2话题: function话题: ptr3话题: char