string

how to make a program which take two string input and tell if a string is found in other string ?
closed account (Dy7SLyTq)
std::cin>> stringOne >> stringTwo;
if(stringOne.find("stringTwo")) std::cout<<"success"<< std::endl;
this is not working
I think he means:
1
2
3
4
5
6
7
8
std::string str1, str2; // 2 strings, input them:
std::cin >> str1 >> str2;

// no quotation marks here, look in both strings
if (str1.find(str2) || str2.find(str1))
    std::cout << "Success" << std::endl; // Found a match
else
    std::cout << "Failure" << std::endl; // No matches 
what do you mean that it's not working, what are the error messages the compiler gives, or events you didn't expect ?

and remember to #include <string>
not give error.but its always remain true and don't go in else part.even i put both string total different.but it give message success.
please help me.how can i done it with char array??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

bool one_contains_the_other( const std::string& first, const std::string& second )
{
    // check if the shorter string is a substring of the longer one
    // find returns std::string::npos on failure
    if( first.size() < second.size() ) return second.find(first) != std::string::npos ;
    else return first.find(second) != std::string::npos ;
}

bool one_contains_the_other( const char* first, const char* second )
{
    if( first == nullptr || second == nullptr ) return true ;
    // forward to the C++ version
    else return one_contains_the_other( std::string(first), second ) ;
}

bool one_contains_the_other_2( const char* first, const char* second )
{
    if( first == nullptr || second == nullptr ) return true ;

    // check if the shorter string is a substring of the longer one
    // strstr returns nullptr on failure
    else if( std::strlen(first) < std::strlen(second) ) return std::strstr(second,first) ;
    else return std::strstr(first,second) ;
}

bool one_contains_the_other_3( const char* first, const char* second )
{
    if( first == nullptr || second == nullptr ) return true ;

    // make first the longer and second the shorter of the two strings,
    if( std::strlen(first) < std::strlen(second) ) std::swap(first,second) ;

    for( ; *first ; ++first ) // hancoded strstr
    {
        if( *first == *second ) // if the first characters match, check for the rest
        {
            const char* s = second ;
            for( const char* p = first ; *p && *s && *p == *s ; ++p, ++s ) ;
            if( *s == 0 ) return true ; // reached the null character, matched
        }
    }
    return false ;
}
Topic archived. No new replies allowed.