Checking If Part Of A String Is There?

This is a discussion on Checking If Part Of A String Is There? within the C++ Programming forums, part of the General Programming Boards category; I was wondering how you check if a certain string is in another string? Like say if you type "cats", ...

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Checking If Part Of A String Is There?

    I was wondering how you check if a certain string is in another string? Like say if you type "cats", how would you determine if "cat" was in that string?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Use the strstr function. If the substring is there, it will be returned as the return value, if it's not there, then it will return NULL

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    The string container offers a find member function that can be used to determine this:
    Code:
    std::string str("cats");
    
    if( str.find("cat") != std::string::npos )
    {
        // Found substring "cat" in str
    }
    else
    {
        // Could not find substring "cat" in str.
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,576
    Code:
    std::string myString = "Hello Nice World!";
    std::string searchfor = "Nice";
    
    std::size_t where;
    
    // starts search at position 0 and returns location where found
    where = myString.find(searchfor, 0);
    Alternatively you can use the find algorithms. Take a look at http://www.cppreference.com/ for C++ strings and Algorithms
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Me
    Join Date
    Jul 2006
    Posts
    71
    Thanks that worked perfectly hk, mario's look's like it has the same idea, so thanks to you too mario.

    Edit:Wait, how do you make it so it checks in multiple words? Like, say I type "the cat drinks milk", it is going to the else, it is not recognizing the string as having the word "cat" in it. Any suggestions?
    Last edited by relyt_123; 07-26-2006 at 04:42 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Alternatively you can use the find algorithms.
    Don't use the find algorithm with string, since it has its own version that may be optimized for string's representation.

    >> Any suggestions?
    Show the code. Replacing std::string str("cats"); with std::string str("the cat drinks milk"); in hk_mp5kpdw's code should work.

  7. #7
    Me
    Join Date
    Jul 2006
    Posts
    71
    Umm...well that's not what I meant. I meant if the user inputs "The cat drinks milk" it won't say that "cat" is in the string, because it is more than one word.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Show the code. If you're trying to say that your input function isn't working when the user enters more than one word, you probably need to use getline to read in the entire line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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