#include <stdio.h>
#include <string.h>
//求字符串中最长单词个数并分别打印出来
int judge( char c )
{
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
return 1;
}
return 0;
}
int word( char * s ,char (*str)[100])
{
int len = 0,count = 0;
char *left = s,*right = NULL;
while( *s != '\0')
{
if( judge(*s) == 1 && judge(*(s-1)) == 0)
{
left = s;
}
if( judge(*s) == 1 && judge(*(s+1)) == 0)
{
right = s;
if( right-left+1 > len)
{
count = 0;
memset( str ,'\0',100*100);
len = right - left + 1;
memcpy( str[count] , left , len);
count++;
}
else if(len == right-left+1)
{
memcpy( str[count] , left , len);
count++;
}
}
s++;
}
return count;
}
int main()
{
char *str = "hello word! I like koko homes";
char s[100][100];
int count = word( str , s );
printf("wordcount = %d\n",count);
int i = 0;
while( i < count )
{
printf("%s\n",*(s+i));
i++;
}
return 0;
}
C语言求字符串中最长单词个数并打印每个单词
于 2021-12-21 22:19:41 首次发布