What is Anagram?
If the characters in one string can be rearranged to form another string, then both the string are anagram to each other.
Example: Eat and Tea are anagram to each other.
Algorithm to check whether the given two strings are anagram:
Step 1: Get the inputs string from the user.
String 1: William Shakespeare
String 2: I am a weakish speller
Step 2: Skip the space in the given input string.
String 1: WilliamShakespeare
String 2: Iamaweakishspeller
Step 3: Convert uppercase characters to lowercase.
String 1: williamshakespeare
String 2: lamaweakishspeller
Step 4: Sort the characters in both the strings.
String 1: aaaeeehiikllmprssw
String 2: aaaeeehiikllmprssw
Step 5: Compare the resultant strings. If both are same, then anagram.
Write a C program to check whether the given two strings are anagram or not.
#include <stdio.h>
#include <string.h>
/*
* skips white space in the given input string. If
* there's any uppercase characters in i/p string
* convert it to lowercase
*/
void skipSpace(char *str) {
char *ptr1, *ptr2;
/* ptr1 and ptr2 points to 1st character of i/p string */
ptr1 = ptr2 = str;
/* copy all characters in the i/p string except space */
while (*ptr1 != '\0') {
/* skip the space */
if (*ptr1 == ' ') {
ptr1++;
continue;
}
/* uppercase characters to lowercase */
if (*ptr1 >= 'A' && *ptr1 <= 'Z') {
/* skip spaces and copying characters */
*ptr2++ = (*ptr1 - 'A') + 'a';
ptr1++;
} else {
/* skip space and copying characters */
*ptr2++ = *ptr1++;
}
}
/* null termination */
*ptr2 = *ptr1;
return;
}
If the characters in one string can be rearranged to form another string, then both the string are anagram to each other.
Example: Eat and Tea are anagram to each other.
Algorithm to check whether the given two strings are anagram:
Step 1: Get the inputs string from the user.
String 1: William Shakespeare
String 2: I am a weakish speller
Step 2: Skip the space in the given input string.
String 1: WilliamShakespeare
String 2: Iamaweakishspeller
Step 3: Convert uppercase characters to lowercase.
String 1: williamshakespeare
String 2: lamaweakishspeller
Step 4: Sort the characters in both the strings.
String 1: aaaeeehiikllmprssw
String 2: aaaeeehiikllmprssw
Step 5: Compare the resultant strings. If both are same, then anagram.
Write a C program to check whether the given two strings are anagram or not.
#include <string.h>
/*
* skips white space in the given input string. If
* there's any uppercase characters in i/p string
* convert it to lowercase
*/
void skipSpace(char *str) {
char *ptr1, *ptr2;
/* ptr1 and ptr2 points to 1st character of i/p string */
ptr1 = ptr2 = str;
/* copy all characters in the i/p string except space */
while (*ptr1 != '\0') {
/* skip the space */
if (*ptr1 == ' ') {
ptr1++;
continue;
}
/* uppercase characters to lowercase */
if (*ptr1 >= 'A' && *ptr1 <= 'Z') {
/* skip spaces and copying characters */
*ptr2++ = (*ptr1 - 'A') + 'a';
ptr1++;
} else {
/* skip space and copying characters */
*ptr2++ = *ptr1++;
}
}
/* null termination */
*ptr2 = *ptr1;
return;
}
/* sort the characters in the input string */
void sortCharacters(char *str) {
int i, j, ch;
for (i = 0; i < strlen(str) - 1; i++) {
ch = str[i];
for (j = i + 1; j < strlen(str); j++) {
if (ch > str[j]) {
ch = str[j];
str[j] = str[i];
str[i] = ch;
}
}
}
return;
}
int main() {
char first[256], second[256];
/* get the first input string from the user */
printf("Enter your first input string:");
fgets(first, 256, stdin);
first[strlen(first) - 1] = '\0';
/* get the second input string from the user */
printf("Enter your second input string:");
fgets(second, 256, stdin);
second[strlen(second) - 1] = '\0';
/*
* skip the whitespace in the i/p strings &
* convert uppercase to lowercase if any.
*/
skipSpace(first);
skipSpace(second);
/* sort the characters in above processed string */
sortCharacters(first);
sortCharacters(second);
/* check data in first and second char array are same */
if (strcasecmp(first, second) == 0) {
printf("Given Two String are Anagram!!\n");
} else {
printf("Give Two Strings are not Anagrams!!\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your first input string:tea
Enter your second input string:eat
Given Two String are Anagram!!
Enter your first input string:tea
Enter your second input string:eat
Given Two String are Anagram!!
No comments:
Post a Comment