Whether the given two strings are anagrams or not in C Programming. We can call to string anagrams when both of them contain same characters but, the order can be different. The inputs necessary for this is, two string which is to be checked for whether they are anagram or not. Our desired output is the validation or conclusion of whether the two strings are anagrams or not.
To read the two strings (c1 and c2) at runtime, we can use gets() function. It is a predefined function used to read input string line by line.
It always continues to read until it reaches a newline or end-of-file. The read input is stored in string pointer or character array.
char c1[150],c2[150];
printf(“Enter a string1 : “);
gets(c1);
printf(“Enter a string2 : “);
gets(c2);
First we check whether the length of two strings are equal using predefined strlen() function of “string.h” library.
If the two lengths are not equal then, the inputs are incorrect so, we end the execution there itself.
if(strlen(c1)!=strlen(c2))
return 0;
Then, we copy second string (c2) into another string c3. We do so because, there are modifications in the string and we might require the initial input for anything in the future. We can copy c2 into c3 using predefined function strcpy() from the “string.h” library.
Then, we iterate from first index to the last (length of string n found using strlen()) of first string (c1) and iterate through the second string (c3) until we find the same character there.
When we find it, we make the character in second string to null indicating it is already matched to one character of first string and increment the count variable (k) which in initially zero by 1.
After finding the matching character, we break the iteration of the second string (c3) and compare for the next index character of first string (c1).
strcpy(c3,c2);
n=strlen(c1);
k=0;
for(i=0;i<n;i++) {
for(j=0;j<n;++j) {
if(c1[i]==c3[j]) {
c3[j]=’\0′;
k++;
}
}
}
Once all the iterations are completed, the value of k represents the number of characters same in both the input strings. So, we check whether this value is equal to the length of the string (n). If they are equal, it means that the two input string are anagrams and we display the same in the output console screen.
Else, if they are not equal then, the two input strings are not anagrams so, we display the same on the output console screen.
if(k==n) {
printf(“both strings are anagrams”);
}
else
printf(“both strings are not anagrams”);
Check if Two Strings Are Anagrams in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <stdio.h> #include <string.h> int main() { char c1[150],c2[150],c3[150]; int i, j,sum=0,k=0,n; printf("Enter a string1 : "); gets(c1); printf("Enter a string2 : "); gets(c2); if(strlen(c1)!=strlen(c2)) return 0; strcpy(c3,c2); n=strlen(c1); for(i=0;i<n;i++) { for(j=0;j<n;++j) { if(c1[i]==c3[j]) { c3[j]='\0'; k++; } } } if(k==n) { printf("both strings are anagrams"); } else printf("both strings are not anagrams"); return 0; } |
Output:
1 2 3 |
Enter a string1 : gate Enter a string2 : tgea both strings are anagrams |
String Anagram Program in C – Using Function
Above we have seen the logic to determine whether the given two input strings are anagrams or not. But a small disadvantage above is that, since the entire code is within main method, no part of the code is reusable and also it is not quite readable.
So instead, if we split the code based on its functionality and place it in separate function blocks it’ll become a lot convenient. If the same logic is required elsewhere in the code then, we can just make a function call and pass the necessary parameters instead of rewriting the same set of statements again.
Also, this way the readability of the code in enhanced because the code is split and placed lot cleaner than before.
So, we initially read the two input strings in main method using gets() and pass them as parameters to the user-defined function(anagram).
Here, the same logic as above is present and it returns 1 if they are anagrams and zero if they are not. In the main method, it the value returned is 1 then, we display that the two strings are anagrams on the console screen else, we display that they are not anagrams.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <stdio.h> #include <string.h> int anagram(char *c1,char *c2) { char c3[150]; int i, j,sum=0,k=0,n; strcpy(c3,c2); if(strlen(c1)!=strlen(c2)) { return 0; } else { n=strlen(c1); for(i=0;i<n;i++) { for(j=0;j<n;++j) { if(c1[i]==c3[j]) { c3[j]='\0'; k++; } } } if(k==n) return 1; else return 0; } } int main() { char c1[150],c2[150]; printf("Enter a string1 : "); gets(c1); printf("Enter a string2 : "); gets(c2); if(anagram(c1,c2)) { printf("both strings are anagrams"); } else { printf("both strings are not anagrams"); } } |