472,352 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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

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
4 8182
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
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
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
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

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

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of...
11
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: ...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored...
15
by: ritesh | last post by:
Hi, I'm working on a piece of code that - 1. has a list of text files 2. and needs to search for a particular expression in these files (this...
0
by: JosAH | last post by:
Greetings, Introduction Last week I started thinking about a text processing facility. I already found a substantial amount of text: a King...
0
by: JosAH | last post by:
Greetings, the last two article parts described the design and implementation of the text Processor which spoonfeeds paragraphs of text to the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.