How to compare substrings with strings?

This is a discussion on How to compare substrings with strings? within the C Programming forums, part of the General Programming Boards category; Ok maybe my topic is extremely vague. For example if I had an 2-d character array called: char testArray[5][11]; which ...

  1. #1
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17

    How to compare substrings with strings?

    Ok maybe my topic is extremely vague.

    For example if I had an 2-d character array called:
    char testArray[5][11];

    which contains:
    1) Hello
    2) Pirate
    3) Beef
    4) Youtube
    5) Random

    And I have another character string which is:
    char compareThis[11] = "eef";

    How can I make a specific code which checks compareThis for all 5 strings.
    And checks that testArray[2] contains "Beef" which contains the substring of "eef"

    Or another case is checking for the substring "ell", which is a substring of testArray[0] that contains "Hello"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Put strstr in a loop.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    Just a question.
    What is a null pointer?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by AsleeKaizoku View Post
    Just a question.
    What is a null pointer?
    A pointer that doesn't point anywhere. Represented by "NULL". Also represented by "0", but people tend to use "NULL" to prove to other people they know what they're doing.

  6. #6
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    How would I compare that?

    ptr = strstr(str1,str2);

    if (ptr != '0')
    ......

    Will this work?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not '0', which is the character '0', but just plain old 0, or just plain old NULL.

  8. #8
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    Im trying to do this...

    Code:
    int main()
    {
    	char str1[5][11];
    	char str2[6] = "Mou";
    	char * cmpValue;
    	int x = 0;
    
    	strcmp(str1[0],"Hello");
    	strcmp(str1[1],"Bigg");
    	strcmp(str1[2],"Mouse");
    	strcmp(str1[3],"Keyboard");
    	strcmp(str1[4],"Pong");
    
    	for (x = 0;x < 5;x++ )
    	{
    		cmpValue = strstr(str1[x],str2);
    
    		if (cmpValue != NULL)
    		{
    			printf("This is it!\n");
    		}
    		else
    		{
    			printf("Nope, not this one\n");
    		}
    	}
    }
    But it just passes everything as "else"

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've managed to put strcmp in place of strcat in several lines of code.

  10. #10
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    OH! Oh my! Thats so embarassing!

    My bad =)

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by AsleeKaizoku View Post
    OH! Oh my! Thats so embarassing!

    My bad =)
    And to make things worse, I said strcat when I obviously meant strcpy.

  12. #12
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    Hahahaha, Yeah I noticed that. I placed STRCPY instead.

    But wouldn't it make a difference in this case?
    Its empty to begin with so I guess strcat is passable right?

  13. #13
    Registered User AsleeKaizoku's Avatar
    Join Date
    Jan 2008
    Posts
    17
    Anyways, I got it working!
    Thanks a bunch guys!

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by AsleeKaizoku View Post
    Hahahaha, Yeah I noticed that. I placed STRCPY instead.

    But wouldn't it make a difference in this case?
    Its empty to begin with so I guess strcat is passable right?
    They're not empty, they're filled with randomness. Not the same thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 06:47 PM
  3. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 01:51 PM
  4. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 01:27 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 10:17 PM

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