adding # of spaces in string to map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drjay1627
    New Member
    • Nov 2008
    • 19

    adding # of spaces in string to map

    hello,

    This is my 1st post here!

    *welcome drjay*

    Thanks! I look answering questions and getting answers to other!

    Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:

    string = "hello world! hello world... I'm a live"

    map<string,int>

    hello 2
    wold 2
    ! 1
    . 3
    Im 1
    ' 1
    a 1
    live 1
    6

    This is how the output should look like when the map is looped/traversed...

    This is part of a bigger project... this is how I tackle word and punctuations.

    Read the string, call method on the string that removes all punctuations. call a second method on the original string to filter all punctuations.

    split the two strings using the space as delimiter/splitter, add to a vector<string> (this vector will obviously have repeats).

    call method addtomap(vector ,map)
    loop through the vector
    check if vector[i] is there in the map
    if found second++
    else insert(vector[i],1)

    the way i have it setup for spaces is i call a method that returns an int, the number of spaces.

    create a new string of size 'number of spaces'
    string spaceLine;
    int noofspaces;
    spaceLine.inser t(0, noofspaces, ' ');

    basically SpaceLine is a string of spaces.

    then i call a method addSpacesToMap( string,map)
    loop through the string, check if " " is present in the map
    if found second++
    else insert(" ",1)
    =============== =========error= =============== =============
    test1.cpp: In function `void addSpacesToMap( std::string, std::map<std::s tring, int, std::less<std:: string>, std::allocator< std::pair<const std::string, int> > >&)':
    test1.cpp:96: error: invalid conversion from `char' to `const char*'
    test1.cpp:96: error: initializing argument 1 of `std::basic_str ing<_CharT, _Traits, _Alloc>::basic_ string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc = std::allocator< char>]'
    =============== =========error= =============== =============

    =============== ==my code =============== =============== ===
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <map>

    using namespace std;

    typedef map<string,int> ::iterator itermap;

    string parsingForPunct uations(string line);
    int parsingForSpace s(string line);
    void addToMap(const vector<string> &word, map<string,int> &map);
    void addSpacesToMap( const string &space, map<string,int> &map);

    int main(){
    map<string,int> mymap;
    string str = "Hello! hello world... I'm I'd @ 12+3 (hello)";
    //string newstr = parsingForPunct uations(str);
    int noofspaces = parsingForSpace s(str);
    cout << "str: " <<str<<endl;
    //cout << "newstr: " <<newstr<<end l;
    cout << "# of spaces: " <<noofspaces<<e ndl;

    string spaceLine;
    vector<string> tempVec;

    tempVec.push_ba ck("hello");
    tempVec.push_ba ck("world");
    tempVec.push_ba ck("apple");
    tempVec.push_ba ck("cat");
    tempVec.push_ba ck("hello");
    tempVec.push_ba ck("world");
    tempVec.push_ba ck(" ");
    tempVec.push_ba ck(" ");

    spaceLine.inser t(0, noofspaces, ' ');
    cout << "Spaces Line:" <<spaceLine<<"e ndl"<<endl;

    for(int i = 0 ; i < tempVec.size(); i++){
    cout << tempVec[i]<<endl;
    }
    addToMap(tempVe c,mymap);
    addSpacesToMap( spaceLine,mymap );

    return 0;
    }

    string parsingForPunct uations(string line){
    string str;
    for(int i = 0 ; i<line.size() ; i++){
    char j = line[i];
    if(j>=33 && j<=47){
    str = str + " " +line[i];
    }
    }
    return str;
    }

    int parsingForSpace s(string line){
    int spaces = 0;
    for(int i = 0 ; i<line.size() ; i++){
    spaces += (line.at(i)==' ');
    }
    return spaces;
    }

    void addToMap(const vector<string> &word, map<string,int> &map){

    itermap it;

    for(int i = 0 ; i < word.size() ; i++){
    it = map.find(word[i]);
    if(it!=map.end( )){
    it->second++;
    }else{
    map.insert(pair <string,int>(wo rd[i],1));
    }
    }
    }
    void addSpacesToMap( string space, map<string,int> &map){

    itermap it1;

    for(int i = 0 ; i < space.length() ; i++){
    it1 = map.find(space[i]);
    if(it1!=map.end ()){
    it1->second++;
    }else{
    map.insert(pair <string,int>( " ",1));
    }
    }
    }
    =============== =======my code=========== =============== ==

    if you comment out the addSpacesToMap( ), program will compile. also this just a segment of my program...

    this is probably the longest 'question' here... but if you get to this point and you are reading this, you only need to help me out with one method and one error:
    error: invalid conversion from `char' to `const char*'

    thanks in advance

    drjay
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
    Code:
    space.substr(i, i + 1)
    I don't know if this will make your code work correctly, but it will clear up the error.
    By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
    Hope this helps.

    Comment

    • drjay1627
      New Member
      • Nov 2008
      • 19

      #3
      Originally posted by boxfish
      space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
      Code:
      space.substr(i, i + 1)
      I don't know if this will make your code work correctly, but it will clear up the error.
      By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
      Hope this helps.
      I'll try that mate thanks!

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        In your example the substring "I'm" is broken into the following words:
        > one instance of single-quote (')
        > one instance of "Im".

        Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.

        Comment

        • drjay1627
          New Member
          • Nov 2008
          • 19

          #5
          Originally posted by donbock
          In your example the substring "I'm" is broken into the following words:
          > one instance of single-quote (')
          > one instance of "Im".

          Are you sure that's what you want? I could understand this input being interpreted as one word ("I'm") or three words ("I", "'", "m"), but I'm quite surprised that you want to pluck a punctuation mark from the middle of a word.
          This is not my assignment mate... yeah this is how my instructor wants... basically I making a word frequency map to be encoded using a huffman code.

          project is read in a very large text file, and encode it.

          Comment

          • drjay1627
            New Member
            • Nov 2008
            • 19

            #6
            Originally posted by boxfish
            space is a string, but space[i] is a char. Try using the string's substr method to get a string that's one character long at position i:
            Code:
            space.substr(i, i + 1)
            I don't know if this will make your code work correctly, but it will clear up the error.
            By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks. And there are a lot of posts here that are longer than yours.
            Hope this helps.

            i did what you said this is the output i get:

            vector "hello"
            vector "world"
            vector "apple"
            vector "cat"
            vector "hello"
            vector "world"
            vector " "
            vector " "
            vector " "
            vector " "
            vector " "
            vector " "
            vector " "
            map 2
            map 2
            map 2
            map 1
            map apple 1
            map cat 1
            map hello 2
            map world 2

            i changed it a bit. rather than calling a method addSpaceToMap() i loop through the string of spaces and i add to tempVec (if you remember this is a vector).

            Code:
            for(int x = 0 ; x < spaceLine.length() ; x++){
            		cout <<spaceLine[x]<<endl; 
            		tempVec.push_back(spaceLine.substr(x, x+1));
            	}

            there are spaces of different size.... any suggestion to fix it?!?

            you dont see it in the output but quotes are of different sizes...

            also thanks for fixing the error!

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              I am using this code in the main function,
              Code:
                  spaceLine.insert(0, noofspaces, ' ');
                  cout << "Spaces Line:" << spaceLine <<"endl"<<endl;
              
                  for(int i = 0 ; i < tempVec.size(); i++){
                      cout << "\"" << tempVec[i] << "\"" << endl;
                  }
                  addToMap(tempVec,mymap);
                  for(int x = 0 ; x < spaceLine.length() ; x++){ 
                      cout << "\"" << spaceLine[x] << "\"" << endl;  
                      tempVec.push_back(spaceLine.substr(x, x+1)); 
                  }
              and I am not getting spaces of different widths.
              So can you please post some more of the modifications you have made?

              Comment

              • drjay1627
                New Member
                • Nov 2008
                • 19

                #8
                Originally posted by boxfish
                I am using this code in the main function,
                Code:
                    spaceLine.insert(0, noofspaces, ' ');
                    cout << "Spaces Line:" << spaceLine <<"endl"<<endl;
                
                    for(int i = 0 ; i < tempVec.size(); i++){
                        cout << "\"" << tempVec[i] << "\"" << endl;
                    }
                    addToMap(tempVec,mymap);
                    for(int x = 0 ; x < spaceLine.length() ; x++){ 
                        cout << "\"" << spaceLine[x] << "\"" << endl;  
                        tempVec.push_back(spaceLine.substr(x, x+1)); 
                    }
                and I am not getting spaces of different widths.
                So can you please post some more of the modifications you have made?
                I figured it out! But couldn't have done it without your help mate!

                instead of => tempVec.push_ba ck(spaceLine.su bstr(x, x+1));
                I did => tempVec.push_ba ck(spaceLine.su bstr(x, 1));

                When its x+1 the first substring will be an empty string with size 1 an second will be empty string with size 2 and so on... Because we loop through the string.

                tempVec.push_ba ck(spaceLine.su bstr(x, 1)); does what I want it to do. I didn't think about it much after that, because I ran into another problem. I wanted to post this early but bytes was down for a long time on Saturday.

                Again thanks for you help!

                drjay

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Oh, sorry, I see I was wrong about that. Argument 2 is the size of the substring. I'm glad you got it working though, and I'm glad I was of some help.

                  Comment

                  Working...