11 Replies - 3838 Views - Last Post: 27 April 2011 - 12:38 AM Rate Topic: -----

#1 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 02:17 PM

So I'm trying to do a function that will extract words from a given string. So far I have had some success in this task as I've been able to extract all words but the last one. I tried a few things and with different results (infinite loop, program crash, not getting the last word).
My code as it is right now:

#include <iostream>
#include <string>

using namespace std;
int main (){
string input,word;
input="GIVE ME YOUR WORDS";

while (input.find(' ')!=string::npos)
{
	word=input.substr(0,input.find(' '));
	input=input.substr(input.find(' ')+1,input.length());
        cout<<word<<end;
	cin.get(); //This is here only so I can actually see the output
}
cin.get();
return 0;}


The output for this is:
GIVE
ME
YOUR



My best attempt (I think) was using (input.find(' ')!=string::npos) as loop exit, but that ends the loop before I get the last word.

Thank you.

Edit: I should clarify I understand why this doesn't work (no more spaces left on input after I extract "YOUR", so condition is met) but I can't seem to figure out what to do by looking at the string class on cplusplus.com.

This post has been edited by paralaje: 26 April 2011 - 02:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question: Extracting words from string, help with loop exit condition

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1484
  • View blog
  • Posts: 8,203
  • Joined: 07-September 06

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 02:35 PM

If there are no more spaces left in the sentence (assuming you don't care about punctuation) then you simply take whatever is left in the input string (input) and you have your final word (assuming it isn't blank).
Was This Post Helpful? 1
  • +
  • -

#3 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 02:41 PM

Yes, but where do I save it? The exit condition becomes true upon extracting the penultimate word, so I would need another iteration to extract the last one. Right?

EDIT: Should've given your answer more thought before replying. I'm guessing you meant take whatever is left *after* the loop. That should work.

This post has been edited by paralaje: 26 April 2011 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 03:11 PM

I have a follow up question: I do care about punctuation so instead of just:
input.find(' ')
I'm using:
input.find_first_of(" ,.?!")
and I've stumbled upon a different problem. All the words are extracted, but whenever punctuation is found, an unexpected end of line shows up at the output. For example:
input="HELLO, HOW ARE YOU"
gives the following output:

HELLO

HOW 
ARE
YOU

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 03:40 PM

Show your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 03:48 PM

This the code in its current form:
#include <iostream>
#include <string>

using namespace std;
int main (){
string input,word;
input="HELLO, HOW ARE YOU";
	while (input.compare(word)!=0)
	{
		word=input.substr(0,input.find_first_of(" ,.?!"));
		input=input.substr(input.find_first_of(" ,.?!")+1,input.length());
	}

cin.get();
return 0;
}

This post has been edited by paralaje: 26 April 2011 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 04:20 PM

To be able to print the punctuation character you will need to know what was found by your find_firs_of() function. To do this move the find_first_of() function out of the substr() function.

// before your loop
size_t position = 0;
char foundChar = 0;

// in your loop
position = input.find_first_of(" ,.?!"));
if(position != std::string::npos) // insure that you found one of the chars.
   foundChar = input[position];
else
   foundChar = 0; 

 word=input.substr(0,position);
input=input.substr(position+1,input.length());
cout << word << std::endl;
if(foundChar != 0 || foundChar != ' ')
  cout << foundChar << endl;



Jim
Was This Post Helpful? 1
  • +
  • -

#8 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 04:48 PM

Using input="HELLO, HOW ARE YOU"
Tried your code (changed only || to && because I didn't want to see spaces) and it's giving me the output:
HOW
,
ARE
YOU

which I suppose is correct, except I need to extract the words and the words only. I'm obviously missing something here and I think it's related to having two unwanted characters in a row.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 04:58 PM

Quote

which I suppose is correct, except I need to extract the words and the words only. I'm obviously missing something here and I think it's related to having two unwanted characters in a row.


Yes it is because of the two spaces. So if you find any of the characters other than the space you will need to change the input string, then start the loop over (continue).
input=input.substr(position+1,input.length());
if(foundChar != 0 || foundChar != ' ')
  continue;

cout << word << std::endl;



Jim
Was This Post Helpful? 1
  • +
  • -

#10 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 06:53 PM

When I read your post, I didn't fully understand the code you provided (and still don't, actually) and I started to feel guilty about not solving my own problem so I decided to keep trying on my own.
After a while I seem to have found a solution, though I'll be honest and say I don't really know why this works.

#include <iostream>
#include <string>
#include "archivoTexto.h"

using namespace std;
int main (){
string input,word;
input = "";
unsigned int position = 0;
	while (input.compare(word) != 0)
	{
		position = input.find_first_of(" ,.?!<>/");
		word = input.substr(0,position);
		input = input.substr(position+1, input.length());	
		if (!word.empty())
		cout<< word << endl;
	}

cin.get();

return 0;}


This seems to be working fine, but I still don't get why the string word comes out empty when the program reads one of the unwanted chars.

Thank you very much for your responses.

EDIT: The input string isn't actually empty when I run the program, this is part of a larger code that reads lines from a text file.

This post has been edited by paralaje: 26 April 2011 - 07:00 PM

Was This Post Helpful? 0
  • +
  • -

#11 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1484
  • View blog
  • Posts: 8,203
  • Joined: 07-September 06

Re: Question: Extracting words from string, help with loop exit condition

Posted 26 April 2011 - 07:05 PM

What I would do there (since you have a saved word) is to simply check if the string is empty:
if(!word.empty()){
  cout << word << endl;
}


You could also need to go the check at the end of the loop to make sure that you aren't outputting an additional blank string (if the input string say ends in a .)

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#12 paralaje  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 11

Re: Question: Extracting words from string, help with loop exit condition

Posted 27 April 2011 - 12:38 AM

I'm not sure if my last post went unnoticed but, yes, that's in fact what I ended up doing. It took me a while to understand why the string was coming up empty but I ended up getting it.

Thank you very much for your help, problem solved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1