高手都在忙啥呢?真的期待在原有思路和框架上看到指针数组的牛B运用,也好让我这菜鸟开开眼啊!
问题的关键是在一个字符串中有并列最长单词的时候都能输出,为简单起见,字符串以.结束,也算是有标点吧!省去最长单词在最后面的情况。在线等。高手拜托啦!
问题的关键是在一个字符串中有并列最长单词的时候都能输出,为简单起见,字符串以.结束,也算是有标点吧!省去最长单词在最后面的情况。在线等。高手拜托啦!
程序代码:#include<stdio.h>
void fun1(char *p);
void fun2(char *p);
int alphabet(char c);
char *a[100]={NULL};
char *temp;
int i=0;
int first_max;
int main()
{
int j=0;
char str[1000];
printf("input the string:\n");
gets(str);
temp=(char*)str;
fun1(temp);
while(temp&&*(temp+1)!='\0')
{
fun2(temp);
}
if(*a[0]==NULL)
printf("the string is null!\n");
else
{
printf("the longest word is :\n");
while(a[j])
{
while(alphabet(*a[j]))
{
printf("%c",*a[j]);
a[j]++;
}
j++;
printf("\n");
}
}
return 0;
}
int alphabet(char c)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
return 1;
else
return 0;
}
void fun1(char *p)//找到第一个最长的字符,并把他的长度记录在first_max全局变量中
{
int max=0;
int count=0;
int flag=1;
char *point1=NULL,*point2;
while(*p!='\0')
{
if(alphabet(*p))
{
count++;
flag=0;
}
else
{
flag=1;
}
if(flag)
{
if(count>max)
{
max=count;
point2=p;
point1=p-max;
count=0;
}
else
{
count=0;
}
}
p++;
}
a[i++]=point1;
temp=point2;
first_max=max;
}
void fun2(char *p)
{
int max=0;
int count=0;
int flag=1;
char *point1=NULL,*point2=NULL;
while(*p!='\0')
{
if(alphabet(*p))
{
count++;
flag=0;
}
else
{
flag=1;
}
if(flag)
{
if(count>max&&count==first_max)
{
max=count;
point2=p;
point1=p-max;
count=0;
}
else
count=0;
}
p++;
}
a[i++]=point1;
temp=point2;
}
