How to check if a character exists in a string with C++

1 Answer

0 votes
#include <iostream>
 
bool CharExist(std::string s, char ch) {
    return s.find(ch) != std::string::npos;
}
 
int main()
{
    std::string s = "C++ Programming";
     
    if (CharExist(s, 'P'))
        std::cout << "Found" << "\n";
    else
        std::cout << "Not Found" << "\n"; 
         
    if (CharExist(s, 'p'))
        std::cout << "Found" << "\n";
    else
        std::cout << "Not Found" << "\n"; 
}
  
 
 
 
/*
run:
  
Found
Not Found
  
*/

 



answered Sep 27, 2022 by avibootz
...