天天看點

一個字元串中求最長單詞

在輸入的一個字元串中,找出長度最大的單詞。例如:Alin is a good student

輸出:student 每個單詞使用空格隔開。

/****************最長的單詞***********/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	char str[1000];
	char *except=" ";
	char *p,*q;
	int len=0;
	gets(str);
	if(*str==0)
		return ;
	p=strtok(str,except);
	q=p;
	while(p!=NULL)
	{
		//printf("%s\n",p);
		if(len<=strlen(p))
		{
			len=strlen(p);
			q=p;
		}
		p=strtok(NULL,except);
	}
	printf("%s",q);

	return 0;
}