在字符串中找到最小和最长的单词
我使用了:
保留
用于记录单词的开始在哪里。
position_max
和position_min
用于记录最终字符单词的位置。
Max
和min
用于比较每个单词并找到最长和最小的长度。
lenght
是整个字符串的长度。
#include<limits.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>
int main(void)
{
char *stringa = "alex keep all money";
size_t x;
int keep;
int min;
int max;
int position_max;
int position_min;
int lenght;
keep = 1;
max = 0;
min = INT_MAX;
position_max = 0;
lenght = strlen(stringa);
while(x != lenght + 1)
{
if(isalnum(stringa[x]) != 0)
{
keep += 1;
}
if(isspace(stringa[x]) != 0 || stringa[x] == '\0')
{
if(keep > max)
{
position_max = x;
max = keep;
}
if(keep < min)
{
position_min = x;
min = keep;
}
keep = 0;
}
x++;
}
puts("the longest word:");
for(x = position_max - max; x != position_max; x++)
{
printf("%c",stringa[x]);
}
puts("\n\nthe smallest word:");
for(x = position_min - min; x != position_min; x++)
{
printf("%c", stringa[x]);
}
return 0;
}
I used:
keep
for recording where is the begin of the word.
position_max
and position_min
for recording position of final char's word.
max
and min
for compare every word and find the length of longest and the smallest.
lenght
is the length of entire string.
#include<limits.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>
int main(void)
{
char *stringa = "alex keep all money";
size_t x;
int keep;
int min;
int max;
int position_max;
int position_min;
int lenght;
keep = 1;
max = 0;
min = INT_MAX;
position_max = 0;
lenght = strlen(stringa);
while(x != lenght + 1)
{
if(isalnum(stringa[x]) != 0)
{
keep += 1;
}
if(isspace(stringa[x]) != 0 || stringa[x] == '\0')
{
if(keep > max)
{
position_max = x;
max = keep;
}
if(keep < min)
{
position_min = x;
min = keep;
}
keep = 0;
}
x++;
}
puts("the longest word:");
for(x = position_max - max; x != position_max; x++)
{
printf("%c",stringa[x]);
}
puts("\n\nthe smallest word:");
for(x = position_min - min; x != position_min; x++)
{
printf("%c", stringa[x]);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如其他人已经提到的那样 - 您有非初始化变量的问题(
x
,position_min
),也错过了几个边缘案例:当您在开始/结束时有空格时连续的,这是固定版本:as others already mentioned - you have problems with uninitialized variables (
x
,position_min
), also there are couple edge cases missed: when you have spaces at the start/end or multiple consecutive ones, here is fixed version:该程序至少是因为在此中使用的变量X
并未初始化的
另一个问题是变量的初始化
keep> keep
by1
此可以结果可以结果对于像“ a a a a”这样的字符串,具有最大长度的子字符串将为
“ a” a“
,而不是“ bb”
提供了最初的变量x 是由
0
初始化的,或者字符串从Space Cargue''
喜欢“ A” a“ a“
),则最大substring将为> “”
并使用表达式x = position_max -max
在此循环中再次导致不确定的行为。
另外,当字符串包含相邻的白色空间时,您必须考虑情况。
我将编写一个单独的函数,例如,如下演示程序所示。
程序输出是
如果要使用标准C函数
ISSPACE
,那么函数minmax_string
可以查看以下方式,程序输出与上面显示的相同
The program has undefined behavior at least because the variable x used in this while loop
was not initialized
Another problem is the initial initializing of the variable
keep
by1
This can results for example that for the string like "A BB" the substring with the maximum length will be
"A"
instead of"BB"
provided that initially the variablex
was initialized by0
Or if the string starts from space character' '
like" A"
then the maximum substring will be" "
and using the expressionx = position_max - max
in this for loopagain results in undefined behavior.
Also you have to take into account situations when a string contains adjacent white spaces.
I would write a separate function for example as shown in the demonstration program below.
The program output is
If you want to use the standard C function
isspace
then the functionminmax_string
can look the following wayThe program output is the same as shown above that is