moonskyland
asked on
problem while separating words from sentence
Hello, to extract words from sentence i use this code.
#include <iostream.h>
#include <string.h>
using namespace std;
int main ()
{
int i,k;
string s, word;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
s += ',';
int npos = 0;
int lpos = 0;
while ((npos = (int)s.find_first_of(",", lpos)) != string::npos )
{
word = s.substr(lpos, npos - lpos);
cout << word << endl;
lpos = npos + 1;
}
system("PAUSE");
return 0;
}
Input must be made like this (sentence): dog,cat,food,table,carrot, expert,exc hange.
After last word dot.
And output should be like:
dog
cat
food
table
carrot
expert
exchange
-----------------------
But right now, i get:
dog
cat
foor
table
carrot
expert
exchange.
How to make, that there would be no dot "." after last word? (please write sample)
Also one more question, let say later i will need to check if word has more than 5 letters, how could i do that? i know i can't do just "if (word>5)", because word is declared as string. Please help me, thanks!
#include <iostream.h>
#include <string.h>
using namespace std;
int main ()
{
int i,k;
string s, word;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
s += ',';
int npos = 0;
int lpos = 0;
while ((npos = (int)s.find_first_of(",", lpos)) != string::npos )
{
word = s.substr(lpos, npos - lpos);
cout << word << endl;
lpos = npos + 1;
}
system("PAUSE");
return 0;
}
Input must be made like this (sentence): dog,cat,food,table,carrot,
After last word dot.
And output should be like:
dog
cat
food
table
carrot
expert
exchange
-----------------------
But right now, i get:
dog
cat
foor
table
carrot
expert
exchange.
How to make, that there would be no dot "." after last word? (please write sample)
Also one more question, let say later i will need to check if word has more than 5 letters, how could i do that? i know i can't do just "if (word>5)", because word is declared as string. Please help me, thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> Also one more question, let say later i will need to check if word has more than 5 letters, how could i do that
if(word.size() > 5)
{
// more than 5 chars
}
if(word.size() > 5)
{
// more than 5 chars
}
This goes some way towards fixing your problem...
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string word;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
s += ',';
std::string::size_type npos = 0;
std::string::size_type lpos = 0;
while ((npos = s.find_first_of(",", lpos)) != std::string::npos )
{
word = s.substr(lpos, npos - lpos);
std::string::iterator itr = word.begin();
while(itr != word.end())
{
char c = *itr;
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
++itr;
}
else
{
itr = word.erase(itr);
}
}
std::cout << word << std::endl;
lpos = npos + 1;
}
system("PAUSE");
return 0;
}
NB. Code I post is for example only and is not guaranteed to be defect free!
#include <iostream>
#include <string>
int main ()
{
std::string s;
std::string word;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
s += ',';
std::string::size_type npos = 0;
std::string::size_type lpos = 0;
while ((npos = s.find_first_of(",", lpos)) != std::string::npos )
{
word = s.substr(lpos, npos - lpos);
std::string::iterator itr = word.begin();
while(itr != word.end())
{
char c = *itr;
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
++itr;
}
else
{
itr = word.erase(itr);
}
}
std::cout << word << std::endl;
lpos = npos + 1;
}
system("PAUSE");
return 0;
}
NB. Code I post is for example only and is not guaranteed to be defect free!
ASKER
Thanks for help! :) i`m still learning c++.
You are very welcome :)
To your first question, you are currently taking anything from the start of s (your sentence) up till the first comma then cout that, the issue is that it considers the "." (period) as part of your new word. So try addind that to your find_first_of function i.e.
while ((npos = (int)s.find_first_of(",.", lpos)) != string::npos )
now it will split the sentence on every occurance of a period or comma.
as for your 2nd question the function that you are looking for would be .length i.e.
cout << "The length of word is " << word.length() << " characters.\n";
or
if (word.length()<5)
do whatever...
while ((npos = (int)s.find_first_of(",.",
now it will split the sentence on every occurance of a period or comma.
as for your 2nd question the function that you are looking for would be .length i.e.
cout << "The length of word is " << word.length() << " characters.\n";
or
if (word.length()<5)
do whatever...
ASKER
crazybrker, thank you also for help. checked it works too, i thought it is not possible to write ",." (two elements), i mean, i thought it would work only if word1,.word2,.word3,.and so on.
Thanks for help again! :-)
Thanks for help again! :-)
Of course, the next problem you'll hit is other non-alpha characters will also form part of your word-set so you'll have to list them all in the find_first_of but then they'll be word separators when all you probably want to do is just filter them out. This is why I provided you with a code snippet that shows how to filter these out rather than split on them.
btw, these headers :
#include <iostream.h>
#include <string.h>
are deprecated. Use these instead :
#include <iostream>
#include <string>
#include <iostream.h>
#include <string.h>
are deprecated. Use these instead :
#include <iostream>
#include <string>
As per my example code :)
I know ... Just wanted to say it explicitly.
You just wanted the final word :-p
Mmmm ... did I ? ;)
ASKER
final word is mine :D thanks to all for help
You are very welcome --- doh! :-s
if (word[word.length()-1] == '.')
word = word.substr(0, word.length()-1);