How to case insensitive check if char exist in a string with C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <cctype>

bool CharExistCaseInsensitive(std::string s, char ch) {
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    
    return s.find(tolower(ch)) != std::string::npos;
}

int main()
{
    std::string s = "C++ Programming";

    if (CharExistCaseInsensitive(s, 'P'))
        std::cout << "Found" << "\n";
    else
        std::cout << "Not Found" << "\n"; 
        
    if (CharExistCaseInsensitive(s, 'p'))
        std::cout << "Found" << "\n";
    else
        std::cout << "Not Found" << "\n"; 
}
 



/*
run:
 
Found
Found

*/

 



answered Sep 27, 2022 by avibootz
...