String of Word and Numbers

This is a discussion on String of Word and Numbers within the C++ Programming forums, part of the General Programming Boards category; Is there a way to seperate these? Say an address like 123 Fake St., is it possible to seperate the ...

  1. #1
    Unregistered
    Guest

    String of Word and Numbers

    Is there a way to seperate these? Say an address like 123 Fake St., is it possible to seperate the number, the street name, then st.?


    And what would this be? Int, Char, etc.

  2. #2
    fastmonkey
    Guest
    You can use what is called a "token" I haven't programed in a while so I'm not quite sure how to use it still...but it will read a string up to a space or other desiginated character. If you do a search you should be able to find out how to use it...if not i'll find my book tomorrow and look it up for you...

  3. #3
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Use strtok
    Code:
    char fullstring[100] = "123 Fake St.";
    char *p;
    p = strtok(fullstring, " ");
    int i = 0;
    while (p != NULL)
    {
      cout << "Token " << ++i << " : " << p << endl;
      p = strtok(NULL, " ");
    }
    The output :
    Token 1 : 123
    Token 2 : Fake
    Token 3 : St.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Here is another example.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using std::vector;
    using std::cout;
    using std::iter;
    using std::string;
    
    vector<string> Split( const string&);
    void PrintVec ( const vector<string> &);
    
    int main()
    {
      vector<string> Token;
      string address;
    
      cout << "Enter an address: ";
      getline(cin, address);
      cout << "The address you entered is: " << address << endl;
    
      Token = Split(address);
      PrintVec(Token);
    
      return 0;
    }
    
    vector<string> Split (const string & s)
    {
      vector<string> ret;
      string::size_type i = 0;
    
      while ( i != s.size() )
      {
        //skip leading blanks
        while ( i != s.size() && isspace(s[i]) ) ++i;
    
        //find end of next word
        string::size_type j = i;
    
        while ( j != s.size() && !isspace(s[j]) ) ++j;
    
        //if we found some whitespace characters
        if ( i != j)
        {
          //copy from s starting at i and taking j - i characters
          ret.push_back( s.substr( i , j - i) );
          i = j;
        }
      }
      return ret;
    }
    
    void PrintVec (const vector<string> & Token)
    {
      vector<string>::const_iterator i = Token.begin();
      while ( i != Token.end() )
      {
        cout << "Token: " << *i << endl;
        i++;
      }
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. How to copy a word or numbers from text to other location
    By trancedeejay in forum C Programming
    Replies: 12
    Last Post: 02-09-2006, 05:43 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. Help with a troublesome C++ program
    By Kristina in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 01:28 PM
  5. String break to words and numbers
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 03-16-2002, 02:53 PM

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