一个C题目。不会做,高手来看看你是不是也不会。
编写一个函数,其参数为字符串S,返回值为S中连续非空格字符所组成的子串中最大长字串的长度 如“abc 123#$ 789",中最长非空格字串是”123#$".则函数返回5
提示: 作者被禁止或删除 内容自动屏蔽
#include <stdio.h> #include <string.h> #include <stdlib.h> /* local variables */ char *str = " abc #abdc %%abcol ad asf avi xyzabcd okisok hellosdfjlsdjfk "; char *space = " "; /* local functions */ int GetMaxSubStr(char *str) { char *p1 = str; char *p2 = str; char *p3 = str; /* result str pointer */ int max_len = 0; int cur_len = 0; while (1) { p2 = strstr(p1, space); /* get the space location */ if (p2 != NULL) { cur_len = p2 - p1; if (cur_len > max_len) { max_len = cur_len; p3 = p1; } printf("%d\n", cur_len); p1 = p2; while (*p1 == 0x20) // skip the sequence space, may be some words splited by more than one space. { p1++; } } else { cur_len = 0; cur_len = strlen(p1); /* last words.. */ if (cur_len > max_len) { max_len = cur_len; p3 = p1; } printf("%d\n", cur_len); break; } } // print the result str { int str_idx; for (str_idx = 0; str_idx < max_len; str_idx++) { putchar(*p3++); } printf("\n"); } return max_len; } int main() { GetMaxSubStr(str); // test system("pause"); return 0; }