Latest :

C Program : Check if Two Strings Are Anagram or Not

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

Output:

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.

x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...