Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int BUFFER_SIZE = 80;
- int find(char* str, char ch)
- {
- int find = -1, idx = 0;
- while (str[idx] != '\0') {
- if (str[idx++] == ch) {
- find = idx - 1;
- }
- }
- return find;
- }
- int main()
- {
- char str[BUFFER_SIZE], ch;
- cout << "Input a string:";
- cin.getline(str, BUFFER_SIZE);
- fflush(stdin);
- cin.clear();
- cout << "Input a character:";
- cin >> ch;
- // cout << "\ns: " << str << endl;
- // cout << "ch: " << ch << "|" << unsigned(ch) << endl;
- // cout << "gcount: " << cin.gcount() << endl;
- int idx = find(str, ch);
- if (idx > -1) {
- cout << "index = " << idx << endl;
- } else {
- cout << "not found" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement