Using C++ .. You are asked to develop a clone of the wc utility found in most Un
ID: 3787228 • Letter: U
Question
Using C++ ..
You are asked to develop a clone of the wc utility found in most Unix systems. Your wc utility should display the number of lines, words, and bytes contained in each input file on the standard output. Consider the following content stored in a text file called hello-world.txt me: hello world me: how do you do? world: i am doing great me: are you really? What about global warming? We will use wc utility to count the number of lines, word, and bytes in hello-world.txt as follows: A few items to remember: A word is defined as a string of characters delimited by white space characters. White space characters are the set of characters for which the iswspace(3) function returns true. A line is defined as a string of characters delimited by a character. Characters beyond the final character will not be included in the line count. You are not allowed to use the built-in wc to do the work for you. You are only allowed to use basic C++ functionality including iostream and fstream. You are not to use STL string class.Explanation / Answer
/*here's the c++ code for the wc clone program*/
#include<iostream.h>
#include<fstream.h>
int main()
{
//to open the text file
ifstream fp("hello-world.txt");
int line=1,word=1,size; // it will not count first word and the last line so initial value is 1
char ch;
fp.seekg(0,ios::end); //taking the file pointer to file's end
size=fp.tellg(); //count number of bytes till current postion for file pointer
fp.seekg(0,ios::beg); //bring file pointer to begining to traverse the file again
while(fp)//till the end of file indicated by fin
{
fp.get(ch); //reading each character
if(ch==' ') //if it's a space then increment word count
word++;
if(ch==' ') //if it's new line character, then increment lines
line++;
}
cout<<line<<" "<<word<<" "<<size<<endl;
fp.close(); //close the file
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.