473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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 8428
Jai Vrat Singh
18 New Member
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
bigbagy
4 New Member
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_stri ng<char,struct std::char_trait s<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
Jai Vrat Singh
18 New Member
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_stri ng<char,struct std::char_trait s<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
bigbagy
4 New Member
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
2642
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 (with a key = a string from the list and value = None) for the purpose of insertion and removal? Basically, if Python really implements lists as...
3
1953
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 me give you a little background. this database is kind of like a human resources database. it collects info about people, where they work, and if...
11
2422
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 details on the same line e.g. B1-Title, B1-Initials, B1-Surname, B2-Title, B2-Initials, B2-Surname, etc. all linked to my main borrower table via an...
3
6342
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, tabs, linefeeds, returns). It will print 3 pieces of information before exit: the first word, the last word, and the number of words read, with one...
21
3858
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 applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
15
2106
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) Currently the search is done using the 'grep' utility on linux. This takes too much time, and kills the responsiveness of the application.
0
4036
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 text for the examples in this article. I want to 'transform' the entire text to a Java object that allows me to search through the entire text in a...
0
4070
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 up and stores the text being fed to it. Finally the LibrayBuilder is able to produce a Library which is the topic of this part of the article. ...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.