Created
January 10, 2010 08:49
-
-
Save gfx/273395 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* C++ */ | |
| /* $ g++ foo.cpp && ./a.out */ | |
| #include <iostream> | |
| #include <cstring> | |
| int count(const char *str, char ch); | |
| using namespace std; | |
| int main(void) { | |
| string word; | |
| char ch; | |
| cout << "Input a word> " << flush; | |
| cin >> word;; | |
| cout << "> " << flush; | |
| while(cin >> ch) { | |
| int num = count(word.c_str(), ch); | |
| cout << "\n" << '"' << word << '"' << " have " << num << " " << ch << endl; | |
| cout << "> " << flush; | |
| } | |
| return 0; | |
| } | |
| int count(const char *str, char ch) { | |
| int i, len, cnt = 0; | |
| len = strlen(str); | |
| for (i = 0; i < len; i++) { | |
| if (*(str+i) == ch) { | |
| cnt++; | |
| } | |
| } | |
| return cnt; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment