473,411 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 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 8386
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 it more efficient to maintain it as a dictionary...
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 information from several different tables. first let...
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 having is that i have 4 different sets of borrower...
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: Words are delimited by white spaces (blanks,...
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 through a character type. I'm not sure if that...
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 is being done on Linux using gcc 3.4.2) ...
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 James version of the bible. I'm going to use that...
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 LibraryBuilder. The latter object organizes, cleans...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.