I need to compare the second word from two sentences using strcmp and then out the words. I have tokenized the strings but don't know how to reference the 2nd word....and then compare. Can anyone give me a clue? Thanks.
I need to compare the second word from two sentences using strcmp and then out the words. I have tokenized the strings but don't know how to reference the 2nd word....and then compare. Can anyone give me a clue? Thanks.
Yeah, umm...
I think that you can create three separate strings:
one for the first word
one for the second word
and one for the rest of the sentence.
However, if you use this method you might want to set 'space' to the 'return'(or 'enter') key. That way the user won't have to hit enter after the first word, but space like he normally would.
Hey, but than again I'm still sort of a newbie.
See if that works.
"All things come to an end"
Just need 2 strings...have 1 string to hold the entire sentence...then go thru the list once, find the first space (start of the 2nd word), and then find the 2nd space (end of the 2nd word) and then just traverse the string like its an array using the start and ending points with a for loop
thanks...i need a bit more help on this one. How do i signify the first space in my for loop? and then once i start and stop the string breakdown how do I compare the two words...any help would be great ....thanks in advance... anyone...
Assumig that you are using null-terminated c-style strings...
You need to make a loop. Then you can search for spaces like this:
if(StringOne[i] == ' ') // That is a space in single-quotes.
Start at i= 0. Incriment i each loop. Loop until i = end of string.
Once you have found the spaces, use another loop and check each character... Something like this:
To find the start of the second word, you should probably check to see if there is more than one space. (Look for a space, then look for a non-space.)Code:for(i = StartOfWord; i < EndOfWord+1; i++) if(FirstString[i] != SecondString[i]) cout << "Words are different" << endl;
[EDIT]
To do this, you need to understand character arrays (c-style strings). i.e. You need to know that if MyString = "Hello World", then MyString[0] = H, and MyString[1] = e. You should know about "null-termination".
In C or C++, 'H' and 'e' will return the ASCII value of the character (And, this is what you need to compare... You don't need to know the ASCII value for this particular exercise.) H would represent a variable, "H" would represent a string-literal. (Not what you want this time.)
Lastly you need to understand loops, because you need to use a loop to check for spaces, and to check each character in the second word to see if it matches.
Last edited by DougDbug; 04-22-2003 at 05:19 PM.
got bored
now all you really have to do is check if they are the same. ...Code:#include <iostream.h> #include <string.h> struct sentence { char sent[80]; char sec_word[20]; int start; int end; }; int main (void) { sentence sent1; sentence sent2; int i = 0, j = 0; sent1.start = 0; sent2.start = 0; sent1.end = 0; sent2.end = 0; cout << "Enter first sentence: "; cin.getline (sent1.sent, 80, '\n'); cout << "Enter second sentence: "; cin.getline (sent2.sent, 80, '\n'); while (sent1.end == 0) { if (sent1.sent[i] == ' ' || sent1.sent[i] == '\n') { if (sent1.start == 0) sent1.start = i; else sent1.end = i; } i++; } i = 0; while (sent2.end == 0) { if (sent2.sent[i] == ' ') { if (sent2.start == 0) sent2.start = i; else sent2.end = i; } i++; } for (i = sent1.start+1; i < sent1.end; i++) { sent1.sec_word[j] = sent1.sent[i]; j++; } sent1.sec_word[j] = '\0'; j = 0; for (i = sent2.start+1; i < sent2.end; i++) { sent2.sec_word[j] = sent2.sent[i]; j++; } sent2.sec_word[j] = '\0'; cout << "\n2nd words are: " << sent1.sec_word << " and " << sent2.sec_word << endl; return 0; }
thanks so much for your help. I have a grasp on loops and identifying nulls. However, (this may be my brain cramping) it seems there should be an easier way to reference a member of a string after it has been tokenized....