C Program to Find the Largest/Longest String

Write a c program to accept n strings and print the string with maximum length i.e write a c program to find the largest/longest string.

Algorithm / Solution:

Here is the solution for C program to find the largest or longest string.
  1. Start
  2. Use 2d string to store n arrays.
  3. Use for loop to loop through strings.
  4. Use strlen() function to check length of each string.
  5. Assign length of string with index 0 to Max.
  6. Compare Max with length of n'th string.
  7. Each time store index of corresponding string when Max changes.
  8. Display string with Max length.
  9. Stop.

C Program / Source Code :

Here is the source code of C program to find the largest or longest string.
  1. /*Aim: Write a program to find the largest or longest string.*/
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define size 100
  5. #define wsize 20
  6. void Longest_Word(char str[][20],int n); // Longest_Word Function Prototype
  7. void main()
  8. {
  9. char str[size][wsize];
  10. int i,count=0,n;
  11. printf("\n How many words to accept:- ");
  12. scanf("%d",&n);
  13. printf("\n Enter %d words:- \n \n",n);
  14. for(i=0;i<n;i++)
  15. scanf("%s",str[i]);
  16. Longest_Word(str,n);
  17. }
  18. // Longest_Word Function
  19. void Longest_Word(char str[][20],int n)
  20. {
  21. int i,Max,len1,c;
  22. Max=strlen(str[0]);
  23. for(i=1;i<n;i++)
  24. {
  25. len1=strlen(str[i]);
  26. if(len1>Max)
  27. {
  28. c=i;
  29. Max=len1;
  30. }
  31. }
  32. printf("\n The longest string among all is \"%s\" \n \n",str[c]);
  33. }
/* Output of above code:- How many words to accept:- 5 Enter 5 words:- Halo I Am Master Chief The longest string among all is "Master"