Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to run on ocelot to find the count of words and optionally the

ID: 3788421 • Letter: W

Question

Write a C program to run on ocelot to find the count of words and optionally the longest and or shortest words in a string input by the user or coming from a file. If there is no filename the user would enter the string right after running the command. You must use getopt to parse the command line.

Usage: words [l] [s] [filename]

The l flag means to find the longest and the s option means to find the shortest. You may have both or one of the flags.
Output should be well formatted and easy to read. Code should be nicely indented and commented. Create a simple Makefile to compile your program into an executable called words.

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
   char *filename = NULL;
   char string[100], word[20], max[20], min[20], c;
   char words[100][100];
   int i = 0, j = 0, flag = 0, count = 0,k;

   if (argc < 2)
   {
       printf("Usage: words l s filename , either l or s or both can be used as command line, filename optional ");
       return -1;
   }
   if (argc == 4)
   {
       filename = (char*)malloc(strlen(argv[3]));
       strcpy(filename, argv[3]);
       printf("%s ",filename);
   }
   if (filename == NULL)
   {
       printf("Enter string: ");
       i = 0;
       do
       {
           //fflush(stdin); // statement 1
           c = getchar(); // statement 2
           string[i++] = c;
       } while (c != ' ');
       string[i - 1] = '';

   }
   else
   {
       FILE *in;
printf("In file reading ");
       in = fopen(filename, "r");
       if (!in)
       {
           printf("Unable to open file for reading ");
           return -1;
       }
       while((c = fgetc(in)) != EOF)
       {
           i = 0;
      
           printf("%c",c);
           string[i++] = c;
          
           if( c == ' ')
           string[i - 1] = '';
       }
      
   }
   if( c == EOF)
   string[i - 1] = '';
   //now construct the array of words
   for (i = 0; i < strlen(string); i++)
   {
       //while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
       while (i < strlen(string) && !isspace(string[i]) )
       {
           word[j++] = string[i++];
       }
       if (j != 0)
       {
          
           word[j] = '';
           strcpy(words[count], word);
           ++count;
           j = 0;
       }
   }
   printf("Words are : ");
   for (i = 0; i < count; i++)
   {
       printf("%s ", words[i]);
   }
  
while( (k = getopt (argc, argv, "l:s")) != -1)
   {
   printf(" c = %c ",k);
       switch (k)
       {
       case 'l':
       //find largest
           for( i = 0; i < count ; i++)
           {
               for( j = i+1; j < count ; j++)
               {
                   if( strlen(words[j]) < strlen(words[j+1]) )
                   {
                       //assign word at j+1 as max
                       strcpy(max,words[j+1]);
                   }
               }
           }
           printf("Largest word :%s ", max);
           break;
       case 's':
       //find smallest
               for( i = 0; i < count ; i++)
           {
               for( j = i+1; j < count ; j++)
               {
                   if( strlen(words[j]) < strlen(words[j+1]) )
                   {
                       //assign word at j+1 as max
                       strcpy(min,words[j]);
                   }
               }
           }
           printf("Shortest word :%s ", min);
           break;
       default:
           printf("command line option not find ");
           break;
       }
   }

}

--------------------------------------------------------------------------

//output

./main -l

Enter string: This is to test the string!!                                                                                                                                      

Words are : This is to test the string!!                                                                                                                                        

c = l                                                                                                                                                                           

Largest word :string!!

------

main -s                                                                                                                                                                 

Enter string: This is to test                                                                                                                                                   

Words are : This is to test                                                                                                                                                     

c = s                                                                                                                                                                           

Shortest word :to

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote