[root@bjxdurs235 20090717]# cat -n revers_article.c
1 #include
2 #include
3
4 void revers_str(char *string, int length)
5 {
6 int i;
7 for ( i = 0; i < length/2; i++ )
8 {
9 char ch;
10 ch = *(string + i);
11 *(string + i) = *(string + length -1 -i);
12 *(string + length -1 -i) = ch;
13 }
14
15
16 }
17
18 int main( int argc,char* argv[] )
19 {
20 //char article[200] = "hello world , i am a programer !";
21 char* article=argv[1];
22 char * str;
23 str = article;
24 //char * all_article_pointer = article;
25 int article_length = strlen(str);
26 printf("length:%d\n",article_length);
27 revers_str(str,article_length);
28
29 //printf("%s\n",str);
30
31 while(*str != '\0')
32 {
33 int length = 0;
34 while( (*str != ' ') && (*str != '\0') )
35 {
36 length++;
37 // printf("length:%d\n",length);
38 str++;
39 }
40 revers_str(str-length,length);
41 if (*str == ' ')
42 str++;
43
44 }
45
46 printf("%s\n",str-article_length);
47
48 }
[root@bjxdurs235 20090717]# ./a.out "are you learning c now ?"
length:24
? now c learning you are
说说这个微软的面试题学到的:
1、思想很重要,先把全文反转一遍,然后再把每个单词反转一遍;
2、在反转单词的时候,我的第二层判断少判断了'\0',结果到了结尾以后,因为不是' ',所以一直执行下去,bug很严重;
3、对于指针的理解,比以前更熟悉了;
4、往main里传参数的方式,虽然很简单,这是第一次使用。
阅读(571) | 评论(0) | 转发(0) |