简体   繁体   English

查找输入数组中最长和最短的字符串

[英]Finding the longest and shortest string in an inputted array

This is my first time using SO but Im having trouble with my simple program.这是我第一次使用 SO,但我的简单程序遇到了问题。 Im learning C and Im trying to sort an array of 10 strings (that the user inputs) to find the largest and smallest string and print that on screen at the end, as well as the length of the string.我正在学习 C 并尝试对 10 个字符串(用户输入)的数组进行排序,以找到最大和最小的字符串,并在最后在屏幕上打印,以及字符串的长度。 My maximum length is running fine however Im having difficulty when trying to find the minimum length, it just keeps saying " The smallest word is '≡■`☻♦' with 4 characters."我的最大长度运行良好,但是我在尝试找到最小长度时遇到了困难,它只是一直说“最小的单词是 '≡■`☻♦',有 4 个字符。” I have tried to create a nested loop for the minimum length so that it compares each string in the array against the next as well as the minimum that is stored in the minLength variable.我试图为最小长度创建一个嵌套循环,以便它将数组中的每个字符串与下一个字符串以及存储在 minLength 变量中的最小值进行比较。 I have no clue why these random symbols are appearing and where its getting this from.我不知道为什么会出现这些随机符号以及它是从哪里得到的。 Any advice would be much appreciated!任何建议将不胜感激! Thank you谢谢

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

int main()
{
    int maxLength = 0;
    int maxIndex = 0;
    int minIndex = 0;
    char word_length[10][20];

    printf("Please type 10 words: --------------------- \n \n");
    /*USER ENTERS 10 WORDS*/
    for (i = 0; i < 10; i++)
    {
        scanf("%s", &word_length[i]);
    }

    /*SEARCH FROM INDEX POINTER 0-10*/
    for (i = 0; i < 10; i++)
    {
        /*NEW VARIABLE LENGTH IS UPDATED TO LENGTH OF EACH STRING HELD IN EACH POINTER IN WORD_LENGTH ARRAY*/
        int length1 = strlen(word_length[i]);
        /*IF THE NEW LENGTH IS BIGGER THAN THE PREVIOUS MAXIMUM LENGTH, THE MAXIMUM LENGTH SCORE IS UPDATED*/
        /*THE MAXINDEX VARIABLE KEEPS A RECORD OF WHICH ARRAY POSITION THIS STRING IS HELD IN*/
        if (length1 > maxLength)
        {
            maxLength = length1;
            maxIndex = i;
        }
        /*NEW VARIABLE LENGTH IS UPDATED TO LENGTH OF EACH STRING HELD IN EACH POINTER IN WORD_LENGTH ARRAY*/
        int length2 = strlen(word_length[i]);
        int next_length2 = strlen(word_length[++i]);
        int minLength = strlen(word_length[1]);
        /*IF THE NEW LENGTH IS SMALLER THAN THE PREVIOUS MINIMUM LENGTH, THE MINIMUM LENGTH SCORE IS UPDATED*/
        /*THE MININDEX VARIABLE KEEPS A RECORD OF WHICH ARRAY POSITION THIS STRING IS HELD IN*/
        if (length2 < next_length2)
        {
            if (length2 < minLength)
            {
                minLength = length2;
                minIndex = i;
            }
        }
    }
    
    /*THE BIGGEST WORD IS DISPLAYED ON SCREEN USING THE MAXINDEX POINTER*/
    printf("The biggest word is '%s' with %d characters\n", word_length[maxIndex], maxLength);
    printf("The smallest word is '%s' with %d characters\n", word_length[minIndex], minLength);

    return 0;
}

For starters this call of scanf对于初学者来说,这个 scanf 调用

scanf("%s",&word_length[i]);

should be rewritten at least like至少应该重写

scanf("%s", word_length[i]);

Though it would be safer to write虽然写起来会更安全

scanf("%19s", word_length[i]);

The both for loops like this像这样的两个for循环

for (i=0;i<11;i++)

use an incorrect condition.使用不正确的条件。

You have to write你必须写

for ( i=0; i < 10;i++)

The variable minLength is not declared in the block scope of the function main.变量minLength未在 function main 的块 scope 中声明。

In this the for loop used for searching a string with the minimal length there is used a locally declared variable minLength在这个用于搜索具有最小长度的字符串的 for 循环中,使用了一个本地声明的变量 minLength

for (i = 0; i < 11; i++)
{
    /*NEW VARIABLE LENGTH IS UPDATED TO LENGTH OF EACH STRING HELD IN EACH POINTER IN WORD_LENGTH ARRAY*/
    int length2 = strlen(word_length[i]);
    int next_length2 = strlen(word_length[++i]);
    int minLength = strlen(word_length[1]);
    ^^^^^^^^^^^^^ 

There is no need to use two for loops.无需使用两个 for 循环。 It is enough to use one loop.使用一个循环就足够了。

It can look the following way它可以如下所示

size_t maxIndex = 0;
size_t minIndex = 0;
size_t maxLength = strlen( word_length[0] );
size_t minLength = strlen( word_length[0] );

for ( size_t i = 1; i < 10; i++ )
{
    size_t length = strlen( word_length[i] );

    if ( maxLength < length )
    {
        maxLength = length;
        maxIndex = i;
    }
    else if ( length < minLength )
    {
        minLength = length;
        mibIndex = i;
    }
}

printf("The biggest word is '%s' with %zu characters\n", word_length[maxIndex], maxLength);
printf("The smallest word is '%s' with %zu characters\n", word_length[minIndex], minLength);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM