Basically, what i would like is if the two letters are attached together (NOT seperated) in the string, it will say good, if not the bad.

Example:

#include <iostream>
#include <string>

int main ()
{
    string letters = "or";
    string test = "The man went to the store.";
    //here is the letters "or"are attatched together in this string, it will out "good" (just for debugging)
    
  system("pause");
  return 0;
}

Well you could do this

size_t pos = 0;
string sample = "The man went to the store.";
while ((pos = sample.find_first_of("o", pos)) != string::npos)
{
    if (sample.find_first_of("r", pos) == pos + 1)
        cout << "good";
}

Sorry I forgot to include a statement to increment the pos variable if it wasn't found. This is the code I used to test it and it works for me now.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    size_t pos = 0;
    string sample = "The man went to the store.";
    while ((pos = sample.find_first_of("o", pos)) != string::npos)
    {
        if (sample.find_first_of("r", pos) == pos + 1)
            cout << "good";
        pos++;
    }
    cin.get();
    return 0;
}

Well you could do this

size_t pos = 0;
string sample = "The man went to the store.";
while ((pos = sample.find_first_of("o", pos)) != string::npos)
{
    if (sample.find_first_of("r", pos) == pos + 1)
        cout << "good";
}

Why?
Wouldn't sample.find_first_of("or", pos) do it in one statement instead of adding another .find plus an increment?

from C++ Reference

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

Find character in string
Searches the string for any of the characters that are part of either str, s or c, and returns the position of the first occurrence in the string.

But Silly me find() has an overload for to search for an entire string within a string. It probably is faster to use than what I thought up.

from C++ Reference

size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;

Find character in string
Searches the string for any of the characters that are part of either str, s or c, and returns the position of the first occurrence in the string.

But Silly me find() has an overload for to search for an entire string within a string. It probably is faster to use than what I thought up.

Didn't realize that about find_first_of, not having used it. Thanks for the clarification. I was certainly thinking there was a method like find that did exactly what was needed. And there it is! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.