- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
C program to check if strings are anagrams or not
In this tutorial, we will learn about how to create a program in C that checks whether any given two strings are anagrams or not.
Check the anagram or not in C.
To check whether any given two strings (by the user at run-time) are anagrams or not in C programming, you have to ask the user to enter the two strings to check and find out whether both strings are anagrams or not, as shown in the program given below.
Two strings will be anagrams of each other if and only if they contain the same number of characters; the order of the characters doesn't matter. That is, if two strings are anagrams of each other, then one string can be rearranged to form the other string. For Example:
- abc and cba are anagrams.
- creative and reactive are anagrams.
- course and source are anagrams.
The two strings saying "super and "upper" are not anagrams. Both strings contain the same number of characters but don't contain each and every character of "super" in "upper" (or "upper" in "super," that is, you cannot rearrange the string "super" to form "upper" or "super," respectively).
Let's take a look at the program to check whether the given two strings are anagrams or not in C. Here is the C anagram program source code:
#include<stdio.h> #include<conio.h> #include<string.h> int main() { char str1[20], str2[20]; int len, len1, len2, i, j, found=0, not_found=0; printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); len1 = strlen(str1); len2 = strlen(str2); if(len1 == len2) { len = len1; for(i=0; i<len; i++) { found = 0; for(j=0; j<len; j++) { if(str1[i] == str2[j]) { found = 1; break; } } if(found == 0) { not_found = 1; break; } } if(not_found == 1) printf("\nStrings are not Anagram"); else printf("\nStrings are Anagram"); } else printf("\nBoth string must contain same number of character to be an Anagram Strings"); getch(); return 0; }
As the above program was written in the Code::Blocks IDE, therefore, after a successful build and run, you will get the following output: Here is the initial snapshot of the sample run:
Now supply any two strings, say social and soliat, as input and press the ENTER key to see the output as shown in the second snapshot of the sample run given here:
Let's take another sample run and check for another two strings, say super and upper. Here is the final snapshot of the sample run:
As you can see from the above output, where the two strings say super and upper, supply The first string, "super," has an extra s and one missing p. And the second string, upper, has a second p as an extra character, and one character, s, is missing from it. Therefore, we cannot rearrange super to form upper (or upper to form super).
Let's check for another two strings, say creative and reactive, using the following sample run. Here is the final snapshot of the sample run:
Here are some of the main steps used in the above program:
- Receive any two strings as input.
- Use the strlen() function to find the length of both strings.
- Then initialize the length of the first string to any variable, say len1, and the length of the second string to any variable, say len2.
- Now check whether the length of both strings is equal or not.
- If both strings don't have the same number of characters, then print any message like Both strings must contain the same number of characters to be anagram strings.
- And if both strings contain the same number of characters, then follow the steps given below to check whether each and every character of the first string is present inside the second string or not.
- Create a for loop that runs from 0 to one less than the string's length (either the first or second because we have checked and found that both strings contain the same number of characters).
- Inside the for loop, initialize 0 to any variable, say found, that increments its value by 1 if a character from the first string gets found in the second string.
- Create another for loop that also runs from 0 to one less than the string length.
- Here we have created two for loops in a way to compare each and every character of the first string to each and every character of the second string.
- While comparing, if a character from the first string gets matched to the character of the second string, then initialize 1 to the found variable and break out of the inner for loop using the break keyword.
- After running the inner for loop, check whether found holds its original value of 0 or not.
- If it holds, then the current character of the first string is not found in the second string. Therefore, we have checked after each and every complete run of the inner for loop to see whether the value found holds its original value or not. If it holds (that is, 0), then we have to initialize 1 to the not_found variable. And get out of the outer for loop. As if any of the characters from the first string does not get found in the second, it means it is not required to check for the remaining characters, as the strings cannot be anagrams of each other if any character from the first string does not get matched with a character in the second string.
- At last, we have checked to see whether the variable say not_found holds its original value of 0 or not.
- If it holds, then both strings are anagrams. Otherwise, both strings are not anagrams (in case it holds the value of 1).
The brief description of the above program is that first we have to check the length of both strings; if length is equal, then proceed further; otherwise, print the message for unequal length. We have compared the first character of the first string to all the characters of the second string one by one, then compared the second character of the first string to all the characters of the second string one by one, then compared the third character of the first string to all the characters of the second string one by one, and so on.
While comparing, if any of the characters of the first string match the characters of the second string, then we have initialized 1 to the found and exit from the inner loop. If there is no match found for any character, then 1 will not be initialized to it. So the found holds its previous or initial value, which is 0.
And after exiting the inner loop, on checking "if (found == 0)", it becomes true, and 1 will be initialized to the variable not_found and exit from the outer loop. Now, after exiting the outer loop, we have to check whether not_found will be equal to 1 or 0. If not_found is equal to 1, then both strings are not anagrams, and if not_found is equal to 0, then both strings will be anagrams.
The same program in different languages
« Previous Program Next Program »