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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigbagy
    New Member
    • Nov 2006
    • 4

    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)
  • Jai Vrat Singh
    New Member
    • Oct 2006
    • 18

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

    Code:
        1  #include<iostream>
         2  #include<fstream>
         3  #include<map>
         4  #include<ctype.h>
         5  
         6   typedef std::map<std::string, int> StrMap; 
         7  void recordData( StrMap& m, std::string& str);
         8  
         9  int main(int argc, char* argv[]){
        10    if(argc != 2) {
        11      std::cerr<<"FIle name required\n";
        12      exit(-1);
        13    }
        14    StrMap stringMap;
        15    std::string str;
        16  
        17    std::fstream in(argv[1], std::ios::in);
        18    while( in>>str){
        19      recordData(stringMap,str);
        20    }
        21    in.close();
        22  
        23    for(StrMap::const_iterator iter=stringMap.begin(); iter != stringMap.end(); iter++){
        24      std::cerr<<"Key= "<<iter->first<<"  Value ="<<iter->second<<"\n";
        25    }
        26    return(0);
        27  }
        28  
        29  
        30  void recordData( StrMap& m, std::string& str){
        31   std::string s;
        32   for(int i=0; i<str.length(); ++i){
        32   for(int i=0; i<str.length(); ++i){
        33     char ch = str[i];
        34     if(std::isspace(ch)){
        35       if(s.length()>0) m[s]++;
        36       s.clear();
        37     } else{
        38       s += tolower(ch) ;
        39     }
        40   }
        41    if(s.length() > 0 ) m[s]++; 
        42  }
        43
    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]

    Comment

    • bigbagy
      New Member
      • Nov 2006
      • 4

      #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_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

      Originally posted by Jai Vrat Singh
      looks like a school assigment which nobody wil want to do..but just FYH

      Code:
          1  #include<iostream>
           2  #include<fstream>
           3  #include<map>
           4  #include<ctype.h>
           5  
           6   typedef std::map<std::string, int> StrMap; 
           7  void recordData( StrMap& m, std::string& str);
           8  
           9  int main(int argc, char* argv[]){
          10    if(argc != 2) {
          11      std::cerr<<"FIle name required\n";
          12      exit(-1);
          13    }
          14    StrMap stringMap;
          15    std::string str;
          16  
          17    std::fstream in(argv[1], std::ios::in);
          18    while( in>>str){
          19      recordData(stringMap,str);
          20    }
          21    in.close();
          22  
          23    for(StrMap::const_iterator iter=stringMap.begin(); iter != stringMap.end(); iter++){
          24      std::cerr<<"Key= "<<iter->first<<"  Value ="<<iter->second<<"\n";
          25    }
          26    return(0);
          27  }
          28  
          29  
          30  void recordData( StrMap& m, std::string& str){
          31   std::string s;
          32   for(int i=0; i<str.length(); ++i){
          32   for(int i=0; i<str.length(); ++i){
          33     char ch = str[i];
          34     if(std::isspace(ch)){
          35       if(s.length()>0) m[s]++;
          36       s.clear();
          37     } else{
          38       s += tolower(ch) ;
          39     }
          40   }
          41    if(s.length() > 0 ) m[s]++; 
          42  }
          43
      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]

      Comment

      • Jai Vrat Singh
        New Member
        • Oct 2006
        • 18

        #4
        Originally posted by bigbagy
        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
        Code:
        #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..

        Comment

        • bigbagy
          New Member
          • Nov 2006
          • 4

          #5
          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
          Originally posted by Jai Vrat Singh
          Try including
          Code:
          #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..

          Comment

          Working...