Question

In: Computer Science

Write a C program to find the count of words and optionally the longest and or...

Write a C program 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.

You should submit the source code and your Makefile. The Makefile should be called Makefile with no extension. I should be able to type make at the command line to compile your program. Do not include any other files or folders in the zipfile. This applies even if you are a Mac user.

Solutions

Expert Solution

Here is the code for your question. Also attached is the screen shot of compiling and running the program with different options . A file test.txt was used and its contents displayed in the output. Please do rate the answer if you are happy with its working

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char **argv)
{
int lflag=0,sflag=0;
char filename[256]="\0",line[256];
FILE *file;
int start=0,end,word_len,max=0,min=0,long_words=0,short_words=0,total=0;
int c,index;

while ((c = getopt (argc, argv, "ls")) != -1)
switch (c)
{
case 'l':
lflag = 1;
break;
case 's':
sflag = 1;
break;


}
for (index = optind; index < argc; index++)
{
//printf("%s",argv[index]);
strcpy(filename, argv[index]);
break;
}
if(filename[0]=='\0')
{
printf("Enter a line:");
//get a line from stdin max length 256
fgets(line,256,stdin);
}
else
{
file=fopen(filename,"r");
fgets(line,256,file);
}

//printf("%s",line);
while(line[start]!='\0')
{
//skip over all space characters to find a new word beginning
while(line[start]==' ' || line[start]=='\t' || line[start]=='\n')
{

start++;

}
end=start;
//find a space character to figure out word end
while(line[end]!=' ' && line[end]!='\t' && line[end]!='\0' && line[end]!='\n')
{

end++;

}
word_len=end-start;
if(word_len==0) /*if start and end are same, i.e on '\0'*/
break;
if(lflag)
{
if(word_len>max) /*if the new word is bigger than older longest*/
{
max=word_len;
long_words=1;
}
else if(word_len==max) /*if the word is same length as longest so far, increase count*/
long_words++;
}
if(sflag)
{
if(min==0 || word_len<min) /*if this is new shortest word*/
{
min=word_len;
short_words=1;
}
else if(word_len==min) /*found another shortest word*/
short_words++;
}
//printf("max:%d min:%d",max,min);
total++;
start=end;


}
printf("\n Total No. of words : %d",total);
if(lflag)

printf("\n Longest length is %d. No. of long words :%d",max,long_words);

if(sflag)
printf("\n Shortest length is %d. No. of short words :%d",min,short_words);


}


Related Solutions

2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
6. Write a function to count a tree’s height, which is the length of the longest...
6. Write a function to count a tree’s height, which is the length of the longest path from the root to a leaf. Please finish this question in python programming
Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
In the language c using the isspace() function: Write a program to count the number of...
In the language c using the isspace() function: Write a program to count the number of words, lines, and characters when user enter statements as input. A word is any sequence of non-white-space characters. Have the program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Example of the user input could be the statement below:              You're traveling through ​              ...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT