Created
December 22, 2025 06:07
-
-
Save fariedrahmat/3f91f5a51fc4de7a630c51d85fea6384 to your computer and use it in GitHub Desktop.
Anagram Completed
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
| #include <stdio.h> | |
| #include <string.h> | |
| #include <ctype.h> | |
| #define CHAR_COUNT 26 | |
| #define CASES 5 | |
| // Fungsi cek anagram (abaikan spasi & simbol) | |
| int isAnagram(char str1[], char str2[]) { | |
| int count[CHAR_COUNT] = {0}; | |
| // Hitung frekuensi string pertama | |
| for (int i = 0; str1[i] != '\0'; i++) { | |
| if (isalpha(str1[i])) { | |
| count[tolower(str1[i]) - 'a']++; | |
| } | |
| } | |
| // Kurangi frekuensi string kedua | |
| for (int i = 0; str2[i] != '\0'; i++) { | |
| if (isalpha(str2[i])) { | |
| count[tolower(str2[i]) - 'a']--; | |
| } | |
| } | |
| // Cek keseimbangan | |
| for (int i = 0; i < CHAR_COUNT; i++) { | |
| if (count[i] != 0) { | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int main() { | |
| char kalimat1[200], kalimat2[200]; | |
| for (int i = 1; i <= CASES; i++) { | |
| printf("\nCase %d\n", i); | |
| printf("Masukkan kalimat pertama : "); | |
| fgets(kalimat1, sizeof(kalimat1), stdin); | |
| printf("Masukkan kalimat kedua : "); | |
| fgets(kalimat2, sizeof(kalimat2), stdin); | |
| if (isAnagram(kalimat1, kalimat2)) { | |
| printf("Hasil: ANAGRAM\n"); | |
| } else { | |
| printf("Hasil: BUKAN anagram\n"); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment