choose the longest word in the line .把字符串中最长的单词输出
2014-08-29 22:25
267 查看
#include<malloc.h>
#include<stdio.h>
int main()
{
char*getstr(void);
// intlongest(char *str,char *word);
intlongest(char string[]);
intalphabetic(char c);
char *str =NULL;char *word = NULL;
intlength,i,j;
printf("Please input one line:\n");
str =getstr();
// word =(char*)malloc(sizeof(char)*1);
// length =longest(str,word);
printf("Thelongest word in thi text is:");
for(i =longest(str),j = 0;alphabetic(str[i]);i++,j++)//input the longestword.
{
printf("%c",str[i]);
}
printf("\nits length is %d",j);
return0;
}
char * getstr(void)
{
char c,*str=NULL;
int n=0;
while (1)
{
c=getchar();
str=(char*)str?realloc(str,sizeof(char)*++n):malloc(sizeof(char)*++n);
if (c=='\n'||c==EOF)
{
str[n-1]='\0';
break;
}//if
str[n-1]=c;
}
return str;
}
int alphabetic(char c)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return 1;
else return0;
}
int longest(char string[])
{
int i,length= 0,flag = 0,place = 0,point;
for(i =0;i<=strlen(string);i++)
{
if(alphabetic(string[i]))
{
if(flag==0)
{
point = i;
flag = 1;
}
else
{
flag++;
}
}
else
{
if(flag>=length)
{
length = flag;
place = point;
}
flag = 0;
}//else
}//for
returnplace;
}//longest
注释掉的是本人写的,同过参数传递的方式获取单词长度length/单词word。测试用例比较简单,目前未发现bug。
未注释掉的则是根据教材修改的,实现的也是同上功能(比教材多一点功能),经过严格的白盒测试,基本无bug。
#include<stdio.h>
int main()
{
char*getstr(void);
// intlongest(char *str,char *word);
intlongest(char string[]);
intalphabetic(char c);
char *str =NULL;char *word = NULL;
intlength,i,j;
printf("Please input one line:\n");
str =getstr();
// word =(char*)malloc(sizeof(char)*1);
// length =longest(str,word);
printf("Thelongest word in thi text is:");
for(i =longest(str),j = 0;alphabetic(str[i]);i++,j++)//input the longestword.
{
printf("%c",str[i]);
}
printf("\nits length is %d",j);
return0;
}
char * getstr(void)
{
char c,*str=NULL;
int n=0;
while (1)
{
c=getchar();
str=(char*)str?realloc(str,sizeof(char)*++n):malloc(sizeof(char)*++n);
if (c=='\n'||c==EOF)
{
str[n-1]='\0';
break;
}//if
str[n-1]=c;
}
return str;
}
int alphabetic(char c)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return 1;
else return0;
}
int longest(char string[])
{
int i,length= 0,flag = 0,place = 0,point;
for(i =0;i<=strlen(string);i++)
{
if(alphabetic(string[i]))
{
if(flag==0)
{
point = i;
flag = 1;
}
else
{
flag++;
}
}
else
{
if(flag>=length)
{
length = flag;
place = point;
}
flag = 0;
}//else
}//for
returnplace;
}//longest
注释掉的是本人写的,同过参数传递的方式获取单词长度length/单词word。测试用例比较简单,目前未发现bug。
未注释掉的则是根据教材修改的,实现的也是同上功能(比教材多一点功能),经过严格的白盒测试,基本无bug。
相关文章推荐
- FCC算法:四、Find the Longest Word in a String -- 找出最长单词
- JavaScript实现-找出最长单词-Find the Longest Word in a String
- Bonfire: Find the Longest Word in a String 找出一句话中最大的单词
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
- [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
- Find the Longest Word in a String
- 逆序输出字符串 print the array in reverse order
- 将字符串中最长的单词输出
- The longest suspension bridge in the world 世界上最长的吊桥
- 寻找字符串中最长单词,并输出!
- 8.10 写一函数,输入一行字符,将此字符串中最长的单词输出。(改)
- 华为上机(2)输出一个字符串中最长的一个单词
- freecodecamp学习--Find the Longest Word in a String
- 查找并输出字符串中最长的单词
- Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度
- 输入一个字符串,输出长度最长的单词
- 输入一个字符串,输出长度最长的单词
- 输出一行字符串中的最长单词---调用函数
- 计算字符串中有多少单词,并输出最长最短的单词。(c++primer 9.39)