Advertisement
Taraxacum

find

Oct 21st, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int BUFFER_SIZE = 80;
  6.  
  7. int find(char* str, char ch)
  8. {
  9.     int find = -1, idx = 0;
  10.  
  11.     while (str[idx] != '\0') {
  12.         if (str[idx++] == ch) {
  13.             find = idx - 1;
  14.         }
  15.     }
  16.  
  17.     return find;
  18. }
  19.  
  20. int main()
  21. {
  22.     char str[BUFFER_SIZE], ch;
  23.  
  24.     cout << "Input a string:";
  25.     cin.getline(str, BUFFER_SIZE);
  26.  
  27.     fflush(stdin);
  28.     cin.clear();
  29.  
  30.     cout << "Input a character:";
  31.     cin >> ch;
  32.  
  33.     // cout << "\ns: " << str << endl;
  34.     // cout << "ch: " << ch << "|" << unsigned(ch) << endl;
  35.     // cout << "gcount: " << cin.gcount() << endl;
  36.  
  37.     int idx = find(str, ch);
  38.  
  39.     if (idx > -1) {
  40.         cout << "index = " << idx << endl;
  41.     } else {
  42.         cout << "not found" << endl;
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement