In this C++ tutorial, you will learn how to check if a given string contains a specific character, with example programs.

Check if the string contains specific character in C++

To check if the given string contains a specific character in C++, you can use find() method of string class. Or you can iterate over the characters in the string, and check if the character in the string equals the specified character.

C++ Check if string contains specific character

1. Checking if string contains character using find() method in C++

In the following program, we are given a string in str and the character in ch. We have to check if the string str contains the character ch using find() method.

Steps

  1. Given an input string in str, and a specific character in ch.
  2. Call the find() method of the string class to search for the character ch within the str.
  3. If the find() method returns a value other than string::npos, it means the specified character was found, indicating that str contains the character ch. Use this information to create a condition that returns true if the string str contains the character ch.
  4. Write a C++ if else statement with the condition created from the previous step.

Program

main.cpp

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hello World";
    char ch = 'W';

    if (str.find(ch) != string::npos) {
        cout << "The string contains the character." << endl;
    } else {
        cout << "The string does not contain the character." << endl;
    }

    return 0;
}

Output

The string contains the character.

Now, let us change the character ch value such that the string str does not contain the specified character, and run the program again.

main.cpp

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hello World";
    char ch = 'P';

    if (str.find(ch) != string::npos) {
        cout << "The string contains the character." << endl;
    } else {
        cout << "The string does not contain the character." << endl;
    }

    return 0;
}

Output

The string does not contain the character.
ADVERTISEMENT

2. Checking if string contains character using For loop in C++

In the following program, we have to check if the string str contains the character ch using C++ For loop.

Steps

  1. Given an input string in str, and a specific character in ch.
  2. Take a boolean variable isCharFound to store the information that if the character is found or not.
  3. Write a For loop to iterate over the characters in the string str. Inside the For loop if the character in the string matches the specified character ch, set isCharFound to true, and break the loop. After the completion of For loop, isCharFound is set to true if specified character ch is in the string str, or else false.
  4. Write a C++ if else statement with isCharFound value as condition and print to output whether the character is present in the string or not.

Program

main.cpp

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hello World";
    char ch = 'P';

    bool isCharFound = false;
    
    for (char c : str) {
        if (c == ch) {
            isCharFound = true;
            break;
        }
    }
    
    if (isCharFound) {
        cout << "The string contains the character." << endl;
    } else {
        cout << "The string does not contain the character." << endl;
    }

    return 0;
}

Output

The string contains the character.

Conclusion

In this C++ Tutorial, we learned how to check if the string contains the specified character using string find() method, or For loop, with examples.