Finding the longest string from a list of ten strings in C?
c, string
Solution
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.
Problem
I will have to take the input from user and find the longest input from those 10 strings.. ``` #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? 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.