p12

 Write a C program to take 5 names as input and print the longest name.

  1. /*
  2.  * C Program to Find the Largest & Smallest Word in a String
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. int main()
  9. {
  10.     char string[100], word[20], max[20], min[20], c;
  11.     int i = 0, j = 0, flag = 0;
  12.  
  13.     printf("Enter string: ");
  14.     i = 0;
  15.     do
  16.     {
  17.         fflush(stdin);
  18.         c = getchar();
  19.         string[i++] = c;
  20.  
  21.     } while (c != '\n');
  22.     string[i - 1] = '\0';
  23.     for (i = 0; i < strlen(string); i++)
  24.     {
  25.         while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
  26.         {
  27.             word[j++] = string[i++];
  28.         }
  29.         if (j != 0)
  30.         {
  31.             word[j] = '\0';
  32.             if (!flag)
  33.             {
  34.                 flag = !flag;
  35.                 strcpy(max, word);
  36.                 strcpy(min, word);
  37.             }
  38.             if (strlen(word) > strlen(max))
  39.             {
  40.                 strcpy(max, word);
  41.             }
  42.             if (strlen(word) < strlen(min))
  43.             {
  44.                 strcpy(min, word);
  45.             }
  46.             j = 0;
  47.         }
  48.     }
  49.     printf("The largest word is '%s' and smallest word is '%s' in '%s'.\n", max, min, string);
  50.  
  51.     return 0;
  52. }
Enter string: amazing programmers exists here
The largest word is 'programmers' and smallest word is 'here' in 'amazing programmers exists here'.

No comments:

Post a Comment