C语言求字符串中最长单词个数并打印每个单词

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值