How to check if char exist 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

Related questions

1 answer 123 views
1 answer 151 views
1 answer 114 views
114 views asked Sep 27, 2022 by avibootz
2 answers 186 views
186 views asked Apr 13, 2020 by avibootz
1 answer 215 views
...