找字符串中最长单词的问题
我看谭浩强的教材直接抄到机器上的。代码如下#include <string.h>
#include <stdio.h>
int main( )
{
int alphabetic( char );
int longest( char[] );
int i;
char line[100];
printf("Input one line:\n");
gets(line);
printf("The longest word is:");
for(i = longest( line ); alphabetic( line[i] ); i++)
printf("%c", line[i]);
printf("\n");
return 0;
}
int alphabetic( char c )
{
if(( c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return ( 1 );
else
return ( 0 );
}
int longest ( char string[] )
{
int len = 0, i, length = 0, flag = 1, place = 0, point;
for(i = 0; i <= strlen(string); i++)
if( alphabetic( string[i] ) )
if( flag )
{
point = i;
flag = 0;
}
else
len++;
else
{
flag = 1;
if(len >= length)
{
length = len;
place = point;
len = 0;
}
}
return ( place );
}
可以运行,但是输入 It's very kind of you 后结果显示最长单词是you .谁能解释下?能说下else len++;这句的作用吗?顺带问下怎么将运行结果的截图发上来?
002.zip
(2.93 KB)