【问题标题】:how to find longest and shortest string in c program?如何在c程序中找到最长和最短的字符串?
【发布时间】:2021-03-18 17:39:51
【问题描述】:

我尝试编写一个程序来使用函数和数组来查找最短和最长的字符串,但程序不起作用。程序不显示订购的功能。 这是我的代码:

#include<stdio.h>
#include<string.h>

void Max(char x[][1000], int n);
void Min(char x[][1000],int n);

void Max(char x[][1000], int n){
    int i,Max,len1,c;
    Max=strlen(x[0]);
    for(i=1;i<n;i++){
        len1=strlen(x[i]);
        if(len1>Max)
        {
            c=i;
            Max=len1;
        }
    }printf("\nthe longest string among all is \"%s\" \n \n",x[c]);
}
void Min(char x[][1000],int n){
    int i,min,len2,d;
    min=strlen(x[0]);
    for(i=1;i<n;i++){
        len2=strlen(x[i]);
        if(len2<min)
        {
            d=i;
            min=len2;
        }
    }
    printf("\n the shortest string among all is \"%s\" \n \n",x[d]);
}
int main(){
    
    int i,jmlh=0,n,z;
    printf("How many name to accept: ");
    scanf("%d",&n);
    char x[n][1000];
    printf("\nEnter %d words: \n");
    for(i=0;i<=n;i++){
        gets(x[i]);
    }
    Max(x,n);
    Min(x,n);
    
    return 0;
}

输入是

3

然后输入:

robin van persie
lionel messi
ronaldo

应该是这样的输出:

the longest string among all is "robin van persie".
the shortest string among all is "ronaldo".

也许有人想帮助我修复这个程序,我真的需要你的意见。谢谢

【问题讨论】:

  • 你没有初始化 c 或 d。请阅读您的编译器警告并修复它们
  • 请注意 The gets() function is so dangerous that it should never be used! 它不再是标准 C 的一部分。任何教你使用它的老师都需要回去重新学习补救(现代)C。当然,使用 char x[n][1000]; 给出了一个数组足够大,您不太可能遇到溢出,但即使数组的大小也不能消除这种可能性。
  • 函数 Max()Min() 应该更相似(len1len2,也不需要 cdMaxmin 在函数中),两个函数都不应该打印任何东西。相反,函数应该返回最长或最短字符串的索引,调用代码 (main()) 应该处理打印。将 I/O(打印)与“计算”分开是良好编程的基本技术。它使计算在其他程序中更接近可重用——这还不是你的主要关注点,但随着时间的推移它会成为一个。
  • 在函数中使用函数名作为变量名也不是一个特别好的主意——即使目的是确保递归是不可能的。您在函数 Max() 中有一个局部变量 Max — 您的皮肤应该会爬行,就好像有虫子试图爬过它一样。
  • 我的 min 函数是否正确会产生最短的字符串?我不确定我的程序

标签: c max min c-strings strlen


【解决方案1】:

变量 c 和变量 d 均未在函数 MaxMin 中初始化。

int i,Max,len1,c;

int i,min,len2,d;

至少像这样改变这些声明

int i,Max,len1,c = 0;

int i,min,len2,d = 0;

你在这个循环中输入了 n + 1 个元素

for(i=0;i<=n;i++){

但只将值 n 而不是 n + 1 传递给函数。

请注意函数gets 非常不安全,C 标准不再支持它。相反,请使用函数fgets

【讨论】:

  • 我的 min 函数是否正确会产生最短的字符串?
  • @pratama 如果您要更新函数,它应该会产生正确的结果。
  • 我应该在哪里添加这个 int i,min,len2,d;? .像这样: void Min(char x[][1000],int n,int int i,int min,int len2,int d;); ?
  • @pratama 你需要将变量c和d初始化为0。有什么不清楚的地方?
  • @pratama 你添加了什么> 在评论中显示你的添加。
【解决方案2】:

试试这个代码:

#include<stdio.h>
#include<string.h>

void Max(char x[][1000], int n);
void Min(char x[][1000],int n);

void Max(char x[][1000], int n){
 int i,Max,len1,c=0; // c=0 (in case x[0] is the max) otherwise x[c] is undefined
 Max=strlen(x[0]);
 for(i=1;i<n;i++){ // i<n because I starts from 0
    len1=strlen(x[i]);
    if(len1>Max)
    {
        c=i;
        Max=len1;
    }
 }printf("\nthe longest string among all is \"%s\" \n \n",x[c]);
}
void Min(char x[][1000],int n){
 int i,min,len2,d=0; // d=0 otherwise if min=x[0], x[d]is undefined
 min=strlen(x[0]);
 for(i=1;i<n;i++){
    len2=strlen(x[i]);
    if(len2<min)
    {
        d=i;
        min=len2;
    }
}
printf("\n the shortest string among all is \"%s\" \n \n",x[d]);
}
int main(){

 int i,n,c;
 printf("How many name to accept: ");
 scanf("%d",&n);
 char x[n][1000];
 printf("\nEnter %d words: \n",n);
 while ((c = getchar()) != '\n' && c != EOF) { }
 for(i=0;i<n;i++){ // i<n because i starts from 0 (and not from 1)
    fgets(x[i],sizeof x[i],stdin); // fgets is more secure than gets
    

 }

 Max(x,n);
 Min(x,n);

 return 0;
}

【讨论】:

  • 你会解释你改变了什么以及为什么改变它吗?我知道变量jmlhz 存在问题,但是它们提供了哪些好处或服务,而不是在使用合理选项编译代码时作为未使用变量的警告来源?还要记住,fgets() 在缓冲区中包含换行符。您可能需要删除它:x[i][strcspn(x[i], "\n")] = '\0'; 是一个很好的方法。
  • while ((c = getchar()) != '\n' && c != EOF) { } 你能解释一下吗
  • 我不明白一行:while ((c = getchar()) != '\n' && c != EOF) { } for(i=0;i
  • while ((c = getchar()) != '\n' && c != EOF) { } 。该指令允许删除缓冲区中的换行符。否则,当你输入一个名字时,下一个输入将是换行而不是下一个名字,你明白吗?
  • 你能用简单的 c 代码语句写吗因为我不懂@nissimabehcera
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 2014-12-11
  • 2013-01-11
  • 2012-09-14
  • 1970-01-01
相关资源
最近更新 更多