Anagram in C
0 241
Anagrams, where the letters of one word can be rearranged to form another word, present an intriguing problem in programming.
Anagrams are intriguing linguistic phenomena where the letters of one word or phrase can be rearranged to form another word or phrase, using all the original letters exactly once.
This concept has fascinated word enthusiasts and puzzle solvers for centuries.
Anagrams are not just entertaining; they also serve as tools for language exploration, memory enhancement, and problem-solving.
Examples:
I love cats. (Anagram: Cats love I.)
The sun is shining. (Anagram: Shining is the sun.)
She likes ice cream. (Anagram: Ice cream she likes.)
Algorithm for Anagrams in C:
Remove Spaces and Punctuation: Eliminate any non-alphabetic characters and spaces from both stings to ensure an accurate comparison.
Convert to Lowercase: Standardize the case by converting both stings to lowercase, allowing for case-insensitive comparisons.
Sort Characters: Sort the characters of both strings alphabetically, facilitating easy comparison.
Compare Strings: After sorting, compare the two stings. If they match, they are anagrams; otherwise, they are not.
Examples:
// Program for Anagrams in C #include<stdio.h>#include<string.h> #include<stdbool.h> // Function to check if a character is a vowel bool isVowel(char ch) { ch = tolower(ch); return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'); } // Function to count vowels and consonants in a sentence void countVowelsAndConsonants(char *sentence, int *vowels, int *consonants) { *vowels = 0; *consonants = 0; bool wordBoundary = true; for (int i = 0; sentence[i] != '\0'; i++) { char ch = tolower(sentence[i]); // Check if the character is an alphabet if (isalpha(ch)) { if (isVowel(ch)) { (*vowels)++; } else { (*consonants)++; } // If a word boundary is encountered, reset wordBoundary flag if (isspace(sentence[i + 1]) || sentence[i + 1] == '\0') { wordBoundary = true; } } else { // If a punctuation mark or space is encountered, set wordBoundary flag wordBoundary = true; } } } int main() { char sentence[1000]; int vowels, consonants; // Input sentence printf("Enter a sentence: "); fgets(sentence, sizeof(sentence), stdin); // Count vowels and consonants countVowelsAndConsonants(sentence, &vowels, &consonants); // Output results printf("Number of vowels: %d\n", vowels); printf("Number of consonants: %d\n", consonants); return 0; }
Output:
Enter a sentence: The sky is blue Number of vowels: 4 Number of consonants: 8
This program counts the number of vowels and consonants in a sentence while considering each word as a separate entity.
It utilizes a flag word boundary to identify the end of each word, ensuring accurate counting even with punctuation marks between words.
Here are two C programs: one to check if two strings are anagrams of each other, and another to generate all possible anagrams of a given string.
Check if Two Strings are Anagrams:
#include<stdio.h>#include<string.h> #include<stdbool.h> // Function to check if two strings are anagrams bool areAnagrams(char *str1, char *str2) { int count[256] = {0}; // Assuming ASCII characters // Calculate frequency of characters in str1 for (int i = 0; str1[i] != '\0'; i++) { count[(int)str1[i]]++; } // Subtract frequency of characters in str2 for (int i = 0; str2[i] != '\0'; i++) { count[(int)str2[i]]--; } // If both strings are anagrams, count array will be all zeros for (int i = 0; i < 257; i++) { if (count[i] != 0) { return false; } } return true; } int main() { char str1[100], str2[100]; printf("Enter first string: "); scanf("%s", str1); printf("Enter second string: "); scanf("%s", str2); if (areAnagrams(str1, str2)) { printf("The strings are anagrams.\n"); } else { printf("The strings are not anagrams.\n"); } return 0; }
Output:
Enter first string: end Enter second string: ned The strings are anagrams.
Generate Anagrams of a String:
#include<stdio.h>#include<string.h> // Function to swap characters at positions i and j void swap(char *x, char *y) { char temp = *x; *x = *y; *y = temp; } // Function to generate all permutations of a string void generateAnagrams(char *str, int start, int end) { if (start == end) { printf("%s\n", str); } else { for (int i = start; i <= end; i++) { swap((str + start), (str + i)); generateAnagrams(str, start + 1, end); swap((str + start), (str + i)); } } } int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); printf("Anagrams of %s are:\n", str); generateAnagrams(str, 0, strlen(str) - 1); return 0; }
Output:
Enter a string: int Anagrams of int are: int itn nit nti tni tin
Share:
Comments
Waiting for your comments