GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ Program to Check Whether Strings are Anagrams or Not
C++ Program to Check Whether Strings are Anagrams or NotIn this article, you will learn and get code to check whether the given two strings by the user are anagrams or not using C++ programming. But before going through the program, let's first understand what an anagram means. What are anagram strings?Two strings will be anagrams of each other if and only if they contain the same number of characters. Note: The order of the characters doesn't matter. If two strings are anagrams, then one string can be rearranged to form the other one. For Example:
Now let's move on to the program. In C++, check for anagram stringsTo check whether the two strings are anagrams or not in C++ programming, you have to ask the user to enter the two strings to start checking for anagrams and display the result on the screen (whether the string is an anagram or not) as shown here in the following program. #include<iostream> #include<string.h> using namespace std; int main() { char str1[20], str2[20]; int len1, len2, i, j, found=0, not_found=0; cout<<"Enter the First String: "; cin>>str1; cout<<"Enter the Second String: "; cin>>str2; len1 = strlen(str1); len2 = strlen(str2); if(len1 == len2) { for(i=0; i<len1; i++) { found = 0; for(j=0; j<len1; j++) { if(str1[i] == str2[j]) { found = 1; break; } } if(found == 0) { not_found = 1; break; } } if(not_found == 1) cout<<"\nStrings are not Anagram"; else cout<<"\nStrings are Anagram"; } else cout<<"\nCharacter count Mismatched!"; cout<<endl; return 0; } This program was built and runs under the Code::Blocks IDE. Here is its sample run: Now supply any two strings, say creative and reactive, and press the ENTER key to see the following output: First, we must compare the lengths of both strings; if they are equal, we can proceed.Otherwise, print the message with an unequal length, like "Character count mismatched!" Because two strings must be anagrams, they must be the same length. Now, one by one, compare the first character of the first string to all of the characters of the second string. Then it compares the second character of the first string to all the characters of the second string. Again, compare the third character of the first string to all the characters of the second string, and so on. While comparing, if any of the characters of the first string get matched to the characters of the second string, then initialize 1 to a variable named "found" and exit the loop. And if there is no match found for any character, then 1 will not initialize to it. So, found will be equal to 0 (its previous or initial value). Therefore, after exiting the inner for loop and checking for the condition if(found==0), it evaluates to be true and 1 gets initialized to not_found, and we exit the outer loop. Therefore, after exiting or completing 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 strings are not anagrams; otherwise, strings are anagrams. Above Program Dry RunFor example, if the user enters two strings as reactive and creative, the dry run of the above program with this input goes like this:
The same program in different languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |