By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Counting a text file words in C++ and C, using lists or hash tables

P: 4
Notes
The programs will be compiled and tested on the machine which runs the
Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used.
A significant amount coding is needed for this assignment.

1.Counting Words in C++

Problem: Write an elegant C++ program to count the number of times each word occurs
in a file. A word is defined as a sequence of characters surrounded by whitespace. Case is
not important, so the word “Run” should be treated the same as “run” or “RUN”.
Input: A text file. Assume the number of words in the input is usually less than 150,000.
The maximum size of a single word is 255 characters.
Output: A printout of all words in the input (one word per line), and their corresponding
reference count. Word ordering is unimportant.
Example: Given the following input text:
See the dog. See the dog run. Run dog, run. That dog can really run.
Your program will produce output resembling the following:
can 1
dog 2
dog, 1
dog. 1
really 1
Run 1
run. 3
See 2
That 1
the 2

Notes:
The program must be written in C++ and will be compiled with g++.
Your code must compile cleanly, must not produce any warnings when –Wall is
specified, and must run without any changes.
Make sure your solution is constructed clearly and idiomatically, so that it adheres
to the commonly accepted definition of good style as discussed in class.
Be sure to properly comment your program; explain how the solution works and
why you selected particular algorithm(s) and data structure(s).
Provide citations to references you may have used in constructing your solution.
Input to the program will come from standard input. Output must be to standard
output. Do not prompt for input, nor produce spurious output.
Your program will be testing in a manner similar to the following (the file names
“input.txt” and “output.txt” are only examples):

2. Counting Words in C

Problem: Redo the previous problem, but instead of an elegant C++ program,
produce an elegant and efficient C program. Since the program is in C, you cannot use
any C++ mechanisms, such as the Standard Template Library (STL) or stream I/O.


Notes:
The program must be written in C and will be compiled with gcc.
The code must compile cleanly, must not produce any warnings when –Wall is
specified, and must run without any changes.
Make sure your solution is constructed clearly and idiomatically, so that it adheres
to the commonly accepted definition of good style as discussed in class.
Be sure to properly comment your program; explain how the solution works and
why you selected particular algorithm(s) and data structure(s).
Provide citations to references you may have used in constructing your solution.
Input to the program will come from standard input. Output must be to standard
output. Do not prompt for input, nor produce spurious output.
Your program will be testing in a manner similar to the following (the file names
“input.txt” and “output.txt” are only examples)
Nov 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: 18
looks like a school assigment which nobody wil want to do..but just FYH

Expand|Select|Wrap|Line Numbers
  1.     1  #include<iostream>
  2.      2  #include<fstream>
  3.      3  #include<map>
  4.      4  #include<ctype.h>
  5.      5  
  6.      6   typedef std::map<std::string, int> StrMap; 
  7.      7  void recordData( StrMap& m, std::string& str);
  8.      8  
  9.      9  int main(int argc, char* argv[]){
  10.     10    if(argc != 2) {
  11.     11      std::cerr<<"FIle name required\n";
  12.     12      exit(-1);
  13.     13    }
  14.     14    StrMap stringMap;
  15.     15    std::string str;
  16.     16  
  17.     17    std::fstream in(argv[1], std::ios::in);
  18.     18    while( in>>str){
  19.     19      recordData(stringMap,str);
  20.     20    }
  21.     21    in.close();
  22.     22  
  23.     23    for(StrMap::const_iterator iter=stringMap.begin(); iter != stringMap.end(); iter++){
  24.     24      std::cerr<<"Key= "<<iter->first<<"  Value ="<<iter->second<<"\n";
  25.     25    }
  26.     26    return(0);
  27.     27  }
  28.     28  
  29.     29  
  30.     30  void recordData( StrMap& m, std::string& str){
  31.     31   std::string s;
  32.     32   for(int i=0; i<str.length(); ++i){
  33.     32   for(int i=0; i<str.length(); ++i){
  34.     33     char ch = str[i];
  35.     34     if(std::isspace(ch)){
  36.     35       if(s.length()>0) m[s]++;
  37.     36       s.clear();
  38.     37     } else{
  39.     38       s += tolower(ch) ;
  40.     39     }
  41.     40   }
  42.     41    if(s.length() > 0 ) m[s]++; 
  43.     42  }
  44.     43  
  45.  
  46.  
Output
[HTML]
/home/jaivrat/cpp/test>cat data
hello mr hello dog
cat ran away with a runaway master
super CaT
/home/jaivrat/cpp/test>./a.exe data
Key= a Value =1
Key= away Value =1
Key= cat Value =2
Key= dog Value =1
Key= hello Value =2
Key= master Value =1
Key= mr Value =1
Key= ran Value =1
Key= runaway Value =1
Key= super Value =1
Key= with Value =1
/home/jaivrat/cpp/test>
[/HTML]
Nov 4 '06 #2

P: 4
Thank you Jai for helping me with this matter.There are tow error messages. Would you please have a look on them? . Because I tried to fix them, but I couldn't. Here are the messages:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
1-(18) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (
or there is no acceptable conversion)

2-(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Cpp1.obj - 2 error(s), 5 warning(s)

Bigbagy

looks like a school assigment which nobody wil want to do..but just FYH

Expand|Select|Wrap|Line Numbers
  1.     1  #include<iostream>
  2.      2  #include<fstream>
  3.      3  #include<map>
  4.      4  #include<ctype.h>
  5.      5  
  6.      6   typedef std::map<std::string, int> StrMap; 
  7.      7  void recordData( StrMap& m, std::string& str);
  8.      8  
  9.      9  int main(int argc, char* argv[]){
  10.     10    if(argc != 2) {
  11.     11      std::cerr<<"FIle name required\n";
  12.     12      exit(-1);
  13.     13    }
  14.     14    StrMap stringMap;
  15.     15    std::string str;
  16.     16  
  17.     17    std::fstream in(argv[1], std::ios::in);
  18.     18    while( in>>str){
  19.     19      recordData(stringMap,str);
  20.     20    }
  21.     21    in.close();
  22.     22  
  23.     23    for(StrMap::const_iterator iter=stringMap.begin(); iter != stringMap.end(); iter++){
  24.     24      std::cerr<<"Key= "<<iter->first<<"  Value ="<<iter->second<<"\n";
  25.     25    }
  26.     26    return(0);
  27.     27  }
  28.     28  
  29.     29  
  30.     30  void recordData( StrMap& m, std::string& str){
  31.     31   std::string s;
  32.     32   for(int i=0; i<str.length(); ++i){
  33.     32   for(int i=0; i<str.length(); ++i){
  34.     33     char ch = str[i];
  35.     34     if(std::isspace(ch)){
  36.     35       if(s.length()>0) m[s]++;
  37.     36       s.clear();
  38.     37     } else{
  39.     38       s += tolower(ch) ;
  40.     39     }
  41.     40   }
  42.     41    if(s.length() > 0 ) m[s]++; 
  43.     42  }
  44.     43  
  45.  
  46.  
Output
[HTML]
/home/jaivrat/cpp/test>cat data
hello mr hello dog
cat ran away with a runaway master
super CaT
/home/jaivrat/cpp/test>./a.exe data
Key= a Value =1
Key= away Value =1
Key= cat Value =2
Key= dog Value =1
Key= hello Value =2
Key= master Value =1
Key= mr Value =1
Key= ran Value =1
Key= runaway Value =1
Key= super Value =1
Key= with Value =1
/home/jaivrat/cpp/test>
[/HTML]
Nov 4 '06 #3

P: 18
Thank you Jai for helping me with this matter.There are tow error messages. Would you please have a look on them? . Because I tried to fix them, but I couldn't. Here are the messages:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
1-(18) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (
or there is no acceptable conversion)

2-(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

Cpp1.obj - 2 error(s), 5 warning(s)

Bigbagy

Try including
Expand|Select|Wrap|Line Numbers
  1. #include<string>
By mistake i missed it. Pls revert back if it works now..
I was using g++ on cygwin it did not give me errors..
Nov 4 '06 #4

P: 4
Hi Jai;
I included it but it didn't work. Maybe because I'm currently working on windows not linux. Is it a problem if I try to compile it on windows?. please be patient with me until we compile it correctly.

Thank you
bigbagy
Try including
Expand|Select|Wrap|Line Numbers
  1. #include<string>
By mistake i missed it. Pls revert back if it works now..
I was using g++ on cygwin it did not give me errors..
Nov 4 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.