fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. int count_x(char* p, char x)
  7.  
  8. // count the number of occurrences of x in p[]
  9.  
  10. // p is assumed to point to a zero-terminated array of char (or to nothing)
  11.  
  12. {
  13.  
  14. // if (p==nullptr) return 0;
  15.  
  16. int count = 0;
  17.  
  18. for (;p!=nullptr; ++p)
  19.  
  20. if (*p==x)
  21.  
  22. ++count;
  23.  
  24. return count;
  25.  
  26. }
  27.  
  28.  
  29. int main() {
  30. // your code goes here
  31.  
  32.  
  33. char* str = "Good morning!";
  34. char c = 'o';
  35.  
  36. std::cout << count_x(str, c) << std::endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 4240KB
stdin
Standard input is empty
stdout
0