简体   繁体   English

从C中的十个字符串列表中找到最长的字符串?

[英]Finding the longest string from a list of ten strings in C?

I will have to take the input from user and find the longest input from those 10 strings.. 我将不得不从用户那里获取输入,并从这10个字符串中找到最长的输入。

#include<stdio.h>
#include<conio.h>

void main() {
char str[10][10]
printf("Enter strings:")
scanf("%s", str)

}

If I take the user input like this, would it store the strings in str two dimensional array? 如果我这样接受用户输入,是否会将字符串存储在str二维数组中? To find out the longest string first I would find the length of each strings and use max_length function to determine the longest string. 要首先找出最长的字符串,我会找到每个字符串的长度,并使用max_length函数确定最长的字符串。

You do not need to store all of the strings, just the longest one entered so far. 您不需要存储所有字符串,只需存储到目前为止输入的最长字符串即可。 Note that you do need to define a maximum length of string to avoid buffer overrun. 请注意,您确实需要定义字符串的最大长度,以避免缓冲区溢出。

For example: 例如:

#define MAX_STRING_SIZE 1024

char last_entered_string[MAX_STRING_SIZE];
char longest_entered_string[MAX_STRING_SIZE] = ""; /* Must be initialized. */

scanf("%1023s", last_entered_string); /* Read one less to allow for
                                         null terminator. */

Use a loop to accept the ten inputs and compare with the longest string. 使用循环接受十个输入并与最长的字符串进行比较。 If the last entered string is longer then copy it into the longest string. 如果最后输入的字符串较长,则将其复制到最长的字符串中。 As this is homework I'll not provide any further code. 由于这是家庭作业,因此我将不再提供任何代码。

No, it won't. 不,不会。 You have to loop through and read all strings. 您必须遍历并读取所有字符串。

for(i=0;i<10;i++)
scanf("%s", str[i]);

Also, you are missing some semi-colons! 另外,您缺少一些分号!

You can find the longest string put and save it for all the string received. 您可以找到放置的最长字符串,并将其保存为所有收到的字符串。

int main()
{
 char *str = NULL;
 char *compare;
 printf("Enter strings:");
 scanf("%s", compare);
 if (strlen(str) < strlen(compare))
    str = strdup(compare);
 return(0);
}

And if you want to store all of users input (considering you can have just 10 string from the user)you can do this : 如果要存储所有用户输入(考虑到用户只能输入10个字符串),则可以执行以下操作:

int main()
{
 char **array;
 char *str;
 int x = 0;
 int shortest;
 array = malloc(sizeof(char*) * 10);
 while (x < 10)
  {
   scanf("%s", str)
   array[x] = strdup(str);
   x++;
  }
 x = 0;
 shortest = x;
 while (x < 10)
  {
   if (strlen(array[x]) > strlen(shortest))
     shortest = x;
   x++;
  }
 return (0);
}

shortest will be the index of the longest string in your array. 最短将是数组中最长字符串的索引。

I hope this will help you. 我希望这能帮到您。

Store all input in an array, then do qsort() it on the array entries length and then take the first (or the last, depending on how you sorted) entry. 将所有输入存储在数组中,然后对数组条目的长度执行qsort() ,然后获取第一个(或最后一个,取决于您的排序方式)条目。

Ok, ok ... - this might be over-engineered ... ;-) 好吧,好吧...-这可能是过度设计... ;-)

I think what you can do is take a nested loop and search for a '\\0' character in the row and run a counter simultaneously. 我认为您可以做的是嵌套循环并在行中搜索'\\0'字符并同时运行计数器。 as soon as you find a '\\0' stop the counter and store the value of counter in a separate array . 找到'\\0'停止计数器并将计数器的值存储在单独的数组中。 so now you will have a array of 10 integers. 所以现在您将拥有10个整数的数组。 Now search for smallest integer in the array and... Bingo! 现在搜索数组中的最小整数,然后... Bingo! The corresponding row will have the shortest string. 相应的行将具有最短的字符串。 I know this approach is very raw but I think it will be helpful for people with only basic knowledge of C. 我知道这种方法很原始,但是对于只有C的基本知识的人来说,它会有所帮助。

The program presented will take 10 input strings from the user and then finally print out the longest string and its length. 呈现的程序将从用户那里获取10个输入字符串,然后最终打印出最长的字符串及其长度。 It will not store any other input strings than the biggest one. 除了最大的输入字符串外,它将不存储任何其他输入字符串。

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

#define MAX_STR_LEN 1024

int main(int argc, char **argv){

    char str_final[MAX_STR_LEN];
    char str_temp[MAX_STR_LEN];
    unsigned int i, j, len_str;
    unsigned int num_string = 10;
    unsigned int len_max = 0;

    for (i=0; i<num_string; i++){
        printf("Enter string number: %d\n", i);
        gets(str_temp);

        for (j=0; str_temp[j]; j++);
        len_str = j;

        if(len_str > len_max){
            len_max = len_str;
            memset(str_final, 0, MAX_STR_LEN);
            memcpy(str_final, str_temp, len_str);
        }else{
            memset(str_temp, 0, MAX_STR_LEN);
        }

    }

    printf("The biggest string is: %s\n", str_final);
    printf("It's size is: %d\n", len_max);
    exit(EXIT_SUCCESS); 
}

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

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