Skip to content

Instantly share code, notes, and snippets.

@gfx
Created January 10, 2010 08:49
Show Gist options
  • Select an option

  • Save gfx/273395 to your computer and use it in GitHub Desktop.

Select an option

Save gfx/273395 to your computer and use it in GitHub Desktop.
/* 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