输入一个字符串,将此字符串中最长的单词输出。要求至少使用一个自定义函数。
若有多个长度相等的最长单词,输出最早出现的那个。这里规定,单词只能由大小写英文字母构成。
输入
Keywords insert, two way insertion sort,
Abstract This paper discusses three method for two way insertion
words. insert, two way sorted.
输出
insertion
discusses
insert
#include<stdio.h>
#include<string.h>
#define max 150 //句子总单词数最大值
#define lmax 25 //句子单个单词字母总数最大值
int isc(char t) //判断一个字符是不是字母
{
if((t<='z'&&t>='a')||(t<='Z'&&t>='A'))
return 1;
else
return 0;
}
int isw(char t[],int b,int e)//判断字符串从下标b开始到下标e是不是一个单词
{
int flag=1;
for(int i=b; i<=e; i++)//下标b开始到下标e,中间有不是字母的,返回
{
if(isc(t[i])==0)
{
return 0;
}
}
if(b>0)//不是字符串第一个字符
{
if(isc(t[b-1])==0&&isc(t[e+1])==0)//下标b-1和下标e+1不是字母,返回
{
return 1;
}
}
else
{
if(isc(t[e+1])==0)
{
return 1;
}
}
return 0;
}
void prin(char t[],int b,int e)//打印字符串从下标b开始到下标e之间的内容
{
for(int i=b; i<=e; i++)
{
putchar(t[i]);
}
printf("\n");
}
int main()
{
char t[max]= {' '};
char wt[max][lmax];
while(t)
{
gets(t);
int count=0;
int len=strlen(t);
int Left=0,Right=0;//左指针Left,右指针Right
int p1=0,p2=0,p=0;;
for(Left=0; Left<len; Left++)
{
for(Right=0; Right<len-Left; Right++)
{
if(isw(t,Left,Left+Right)==1)//判断从左指针到右指针,之间是不是一个单词
{
if(Right>p)
{
p1=Left;
p2=Right;
p=p2;
}
count ++;
break;
}
}
}
prin(t,p1,p1+p2);
}
return 0;
}
https://blog.csdn.net/weixin_44882124/article/details/123593401