strcmp

This is a discussion on strcmp within the C++ Programming forums, part of the General Programming Boards category; I need to compare the second word from two sentences using strcmp and then out the words. I have tokenized ...

  1. #1
    leebean
    Guest

    Exclamation strcmp

    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.

  2. #2
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    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"

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    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

  4. #4
    leebean
    Guest
    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...

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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:
    Code:
    for(i = StartOfWord; i < EndOfWord+1; i++)
          if(FirstString[i] != SecondString[i])
               cout << "Words are different" << endl;
    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.)

    [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.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    got bored

    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;
    }
    now all you really have to do is check if they are the same. ...

  7. #7
    leebean
    Guest

    still doesn't address strtk and strcmp

    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....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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