题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/4ec4325634634193a7cd6798037697a8
#include "cstdio" //#include "cstring" #include "string.h" #include "string" using namespace std; int main() { char buf[1024]; while (fgets(buf, sizeof buf, stdin) != nullptr) { string cond1 = buf; buf[cond1.length() - 1] = '\0'; if ('#' == buf[0]) { break; } cond1 = buf; fgets(buf, sizeof buf, stdin); string text = buf; buf[text.length() - 1] = '\0'; text = buf; for (int i = 0; cond1[i] != '\0'; ++i) { int count = 0; for (int j = 0; text[j] != '\0'; ++j) { if (cond1[i] == text[j]) { count++; } } printf("%c %d\n", cond1[i], count); } } return 0; }