Write a C program to run on terminal find the count of words

Write a C program to run on terminal 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.

Solution

# include <stdio.h>

# include <string.h>

# include <stdlib.h>

char flag = \'-\'; char in[100];

void displayMenu();

int countLongestWords(char in[100]);

int countShortestWords(char in[100]);

void getInput();

int getStringLength();

int main() { int term; getInput();

displayMenu();

scanf(\"%c\",&flag);

if(flag == \'I\'|| flag == \'i\')

{

printf(\"%i\",\"Longest Words: \" ,countLongestWords(in));

}

else if(flag == \'S\' || flag == \'s\')

{

printf(\"%i\",\"Shortest Words: \" ,countShortestWords(in));

}

return 0;

}

void getInput()

{

int count = -0;

char c = \'-\';

int i;

printf(\"\ Please input the string: \");

gets(in);

}

void displayMenu()

{

printf(\"\ Please Select the flag from the followings: \");

printf(\"\ I for longest Words\ S for shortest words\ \");

}

int getStringLength() { return strlen(in);

}

int countLongestWords(char input[100])

{

char temp[100], temp2[100], temp3[100];

int count = -1; int i, len = getStringLength();

for(i=0; i<len; i++)

{

if(input[i] != 32 && input[i] != \'\\0\')

{

temp[i] = input[i];

}

strcpy(temp3, temp2);

strcpy(temp2, temp);

if(strlen(temp3)>strlen(temp))

count++;

}

return count;

}

int countShortestWords(char input[100])

{

char temp[100], temp2[100], temp3[100];

int count = -1;

int i, len = getStringLength();

for(i=0; i<len; i++)

{

if(input[i] != 32 && input[i] != \'\\0\')

{

temp[i] = input[i];

}

strcpy(temp3, temp2);

strcpy(temp2, temp);

if(strlen(temp3)>strlen(temp))

count++;

}

return count;

}

Write a C program to run on terminal find the count of words and optionally the longest and or shortest words in a string input by the user or coming from a fil
Write a C program to run on terminal find the count of words and optionally the longest and or shortest words in a string input by the user or coming from a fil
Write a C program to run on terminal find the count of words and optionally the longest and or shortest words in a string input by the user or coming from a fil

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site