满意答案
路飞的潮人店
.05.10
采纳率:59%等级:9
已帮助:464人
我新写了一个,你看下吧, 主要用到了strtok()这个函数, 这个函数在做字符串切割时很有用的...
/*问题描述:
C语言 在字符串中找到最长单词
*/
#include
#include
#include
#define MAX_STRING_LEN 100
void findTheMaxLen(char* string)
{
char* buf = NULL;
char* temp = NULL;
int preStringLen = 0;
for(temp = strtok(string, " "); temp; temp = strtok(NULL, " "))
{
preStringLen = strlen(temp);
buf = (char*)malloc((preStringLen + 1) * sizeof(char));
memset(buf, '\0', (preStringLen + 1) * sizeof(char));
if(strcmp(temp, buf) > 0)
strcpy(buf, temp);
}
printf("最长的字符串是: %s\n", buf);
free(buf);
buf = NULL;
}
void main()
{
char string[MAX_STRING_LEN] = {'\0'};
printf("请输入一个字符串: \n");
gets(string);
findTheMaxLen(string);
}
00分享举报