/C06005 - Đếm số lần xuất hiện các từ trong xâu
<Problem>
https://code.ptit.edu.vn/student/question/C06005#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <ctype.h>
int main() {
char s[200];
gets(s);
for (int i = 0; i < strlen(s); i++) {
s[i] = tolower(s[i]);
}
char arr[100][100];
int cnt[100] = { 0 };
int n = 0;
const char* delim = " ";
char* token = strtok(s, delim);
while (token != NULL) {
bool check = true;
for (int i = 0; i < n; i++) {
if (strcmp(token, arr[i]) == 0) {
check = false;
cnt[i]++;
break;
}
}
if (check) strcpy(arr[n++], token);
token = strtok(NULL, delim);
}
for (int i = 0; i < n; i++) {
printf("%s %d\n", arr[i], ++cnt[i]);
}
return 0;
}