phrase to words ?

This is a discussion on phrase to words ? within the C++ Programming forums, part of the General Programming Boards category; after i use the getline function to get a phrase. Now how do i break the phrase into words ?...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    phrase to words ?

    after i use the getline function to get a phrase. Now how do i break the phrase into words ?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    there's easier ways, but you could walk the string and look for spaces... then break those up into their own strings...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    but how do i do it ?
    when i encounter the 2nd space, how i know the length between the 1st space and the 2nd space ?

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    a space is one character... one element in the array... just write something like this:
    Code:
    ...
    int count=0;
    int x=0;
    int y=0;
    int someleft=strlen(phrase);
    
    while(count<someleft)
    {
       if(phrase[count]!=' ')
       {
          token[y][x]=phrase[count];
          x++;
       }
       else
       {
          x=0;
          y++;
       }
       count++;
    }
    ...
    this will put the words (tokens) in it's own array... actually, in this case, an array of arrays... it would look like this:
    Code:
    input: The Quick Brown Fox
    
    token[0]:The
    token[0][0]:T
    token[0][1]:h
    token[0][2]:e
    token[1]:Quick
    token[2]:Brown
    token[3]:Fox
    like I said, I think there's better ways than this, so you may want to wait around for a better response, but this is the way I would do it (because it's the only way I know how)... I'm not sure the code I gave you works either, but you should be able to get the general idea from it...

    unless you mean there's several spaces... like something like this:
    Code:
    The           Quick  Brown                                         Fox
    in that case, my code would still work, but inefficiently... it would look more like this:
    Code:
    token[0]:The
    token[1]:
    token[2]:
    token[3]:
    token[4]:
    token[5]:
    token[6]:
    token[7]:
    token[8]:
    token[9]:
    token[10]:
    token[11]:Quick
    token[12]:
    token[13]:Brown
    token[14]:
    ...
    token[56]:fox
    at least that's what I think it will do... but like I said, I didn't check the actual code... I would wait for a better response if I were you... I should probably learn some better ways too... or you could try something with isspace(), like this:
    Code:
    ...
    int count=0;
    int x=0;
    int y=0;
    int someleft=strlen(phrase);
    bool new;
    
    while(count<someleft)
    {
       if(phrase[count]!=' ')
       {
          token[y][x]=phrase[count];
          x++;
          new=true;
       }
       else if(new)
       {
          x=0;
          y++;
          new=false;
       }
       else
          x++;
    
       count++;
    }
    ...
    that might fix the problem with multiple spaces... jeebus... this post is getting a little too long for it's own good...
    Last edited by major_small; 01-18-2004 at 10:22 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I wouldn't use new as variable name, it's a keyword.

    You can use string_tokenizer from the boost library.

    But in this case I suppose it would be simpler to use >> for string input in the first place, so that the stream does the splitting for you. Except that makes you miss the breaking character. Hmm...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah... I missed that... and you can't use new as a variable name... heh... I actually started writing that as psuedo-code, but then decided to just write some dummy code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I always say pseudo-code is evil
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by CornedBee
    I always say pseudo-code is evil
    Then stay on your side of the fence. I find it extremely useful.

    back

    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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