-
October 30th, 2007, 05:21 AM
#1
extract first word in a string
user input: **HELLO**!!
what's suppose to happen:
firstWord = HELLO
inspectThis = **!!
Code:
string extractWord (string& inspectThis)
{
string firstWord;
int i = 0;
while (inspectThis[i] != '\n')
{
if (isalpha(inspectThis[i]))
{
firstWord += inspectThis[i];
inspectThis[i] = NULL;
}
inspectThis[i] = NULL;
i++;
}
return firstWord;
}
this is what i have so far and right now i'm stuck
-
October 30th, 2007, 06:48 AM
#2
Re: extract first word in a string
Hello,
Normally, a word is defined as the first group of characters before the first whitespace character. In your problem, you mean it as the first group of alphabets. Am I right?
It is clear that your logic does not result in what you want. Think this way: Check for each character whether it is alphabet or non-alphabet. If you get an alphabet, add it to the first word. If you do not get an alphabet, there can be two cases: (i) you already have a word filled or (ii) you do not have a word. In the first case, terminate the function and return the word. In the second case advance to the next character.
Here is a simple code which does what is explained above, which comes in place of the while loop in your code:
Code:
for (int count = 0; count < inspectThis.length(); count++)
if (isalpha(inspectThis[count]))
firstWord += inspectThis[count];
else if (firstWord.length() > 0)
break;
Once you find the first word, you can check its position in the main string inspectThis and remove that word.
Code:
int pos = inspectThis.find(firstWord);
inspectThis.erase(pos, pos + firstWord.length());
Hope it is clear.
Regards,
Pravin.
-
October 30th, 2007, 01:37 PM
#3
Re: extract first word in a string
Gort...Klaatu, Barada Nikto!
-
October 30th, 2007, 09:21 PM
#4
Re: extract first word in a string
1) There is rarely a circumstance when you would set the value
to an individual character in a std:;string to NULL. So your logic
would be suspect right there.
2) The algorithm to use :
a) find the first alphabetic character.
b) Since you only want the first word, you would start with that character
and go until you reach a non-alphabetic character.
3) using the standard library, the function almost writes itself (although
the way to find the non-alphabetic character is not obvious).
Code:
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
std::string FirstWord(const std::string & s)
{
using namespace std;
string::const_iterator it1 = find_if(s.begin(),s.end(),::isalpha);
string::const_iterator it2 = find_if(it1,s.end(),not1(ptr_fun(::isalpha)));
return string(it1,it2);
}
int main()
{
std::string s = "***! abc sxyz**";
std::cout << FirstWord(s) << "\n";
return 0;
}
Last edited by Philip Nicoletti; October 31st, 2007 at 06:51 AM.
Reason: added :: in front of isalpha for g++ compatibility
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|