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 127 views
1 answer 155 views
1 answer 119 views
119 views asked Sep 27, 2022 by avibootz
2 answers 190 views
190 views asked Apr 13, 2020 by avibootz
1 answer 219 views
...