working with strings

This is a discussion on working with strings within the C++ Programming forums, part of the General Programming Boards category; i'm using the c++ string class is there a function already written that works with words? for example: string x="My ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    working with strings

    i'm using the c++ string class
    is there a function already written that works with words?
    for example: string x="My dog has fleas"
    int y; string z;
    y = wordcount(x); // 4
    z = word(x,2); // "dog"

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    you can access a count method of the string class.....

    I think its string::length()

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    that counts characters, is there a function which counts words? (see example)

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Sorry

    I dont know of such a function offhand

    You could implement one easy by counting the amount of space char in a string........that's easy enough to do....

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i wrote something (still very buggy), anyone wanna point out possible errors or better ways to do it?

    Code:
    ///////////////////////////////////
    string word(int x,string& y) {
      int letter_count=0;  //in case somebody made count a global variable
      int word_count=0;
      int last_word_place=0;
      int seen_letter=0;
    
    if (y.at(0)) {
     while (1) {
      while (++letter_count<y.length() && y.at(letter_count)!=' ')  if (isalpha(y.at(letter_count))) seen_letter=1;
    
     word_count++;
     if (seen_letter==0 && y.length()==letter_count) return 0;
     if (word_count==x || letter_count==y.length()) return y.substr(last_word_place,letter_count-last_word_place);
     last_word_place=letter_count;
    }
    
    }
    }
    
    int word_count(string& y) {
    
        int letter_count=-1;
        int space_count=1;
    
    while (++letter_count<y.length()) if (y.at(letter_count)==' ')  space_count++;
                      // one to back it to the null-terminating char, another to the last char in the string
    if (y.at(letter_count-1)==' ') space_count--;
    return space_count;
    
    }
    a compiliable version is gonna be attached:
    Attached Files Attached Files

  6. #6
    ygf_person
    Guest
    did some changes, doesn't have problems now.
    Code:
    string word(int x,string& z) {     //x is the word number
     if (z=="") return (string)"";
      string psz=z;     //create a temp, it'll go outta scope so nothing's altered
    
    
     while (psz.at(0)==' ')  psz.erase(0,1);                            //leading spaces
     while (psz.at(psz.length()-1)==' ')  psz.erase(psz.length()-1,1);  //trailing spaces
     while (psz.find("  ",0)!=string::npos)  psz.erase(psz.find("  ",0),1);   //extra spaces
    
     for ( ; ; ) {
      x--;
      if (!x)  return psz.substr(0,psz.find(" ",0));
      if (psz.find(" ",0)==string::npos)  return (string)"";  // ERROR: that word doesnt exist
      psz.erase (0,psz.find(" ",0)+1);
      }
    
    }
    
    int word_count(string& y) {
    
    if (y=="") return (int)0;  // ERROR: an empty string sent
    int space_count=1; //at least one word
    
    string psz=y; //the temp will go out of scope
    
    while (psz.at(0)==' ')  psz.erase(0,1);                            //leading spaces
    while (psz.at(psz.length()-1)==' ')  psz.erase(psz.length()-1,1);  //trailing spaces
    while (psz.find("  ",0)!=string::npos)  psz.erase(psz.find("  ",0),1);   //extra spaces
    while (psz.find(" ",0)!=string::npos)  { space_count++; psz.erase(0,psz.find(" ",0)+1); } // count spaces as they are erased
    return space_count;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  2. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 12:04 PM
  3. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  4. Pointers and Strings
    By stallion in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 03:46 PM
  5. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21