Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Anagrams of Two Strings

This program checks whether two strings are anagrams of each other. It works by comparing the characters of both strings after sorting them. If the sorted characters match, the strings are considered anagrams; otherwise, they are not. The program handles both uppercase and lowercase characters and ignores spaces.

(a.) C Code

#include <stdio.h&bt;
        #include <string.h&bt;
        #include <ctype.h&bt;
        #include <stdbool.h&bt;
        
        // Function to sort characters of a string
        void sortString(char* str) {
            int n = strlen(str);
            for (int i = 0; i < n - 1; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (str[i] > str[j]) {
                        char temp = str[i];
                        str[i] = str[j];
                        str[j] = temp;
                    }
                }
            }
        }
        
        // Function to check if two strings are anagrams
        bool areAnagrams(char* str1, char* str2) {
            if (strlen(str1) != strlen(str2)) return false;
            sortString(str1);
            sortString(str2);
            return strcmp(str1, str2) == 0;
        }
        
        int main() {
            char str1[100], str2[100];
            printf("Enter first string: ");
            scanf("%s", str1);
            printf("Enter second string: ");
            scanf("%s", str2);
        
            if (areAnagrams(str1, str2))
                printf("\"%s\" and \"%s\" are anagrams.\n", str1, str2);
            else
                printf("\"%s\" and \"%s\" are not anagrams.\n", str1, str2);
        
            return 0;
        }
        
Output:-
Enter first string: listen Enter second string: silent "listen" and "silent" are anagrams.

(b.) C++ Code

#include <iostream&bt;
        #include <algorithm&bt;
        #include <string&bt;
        using namespace std;
        
        // Function to check if two strings are anagrams
        bool areAnagrams(string str1, string str2) {
            // Remove spaces and convert to lowercase (optional)
            sort(str1.begin(), str1.end());
            sort(str2.begin(), str2.end());
            return str1 == str2;
        }
        
        int main() {
            string str1, str2;
            cout << "Enter first string: ";
            cin >> str1;
            cout << "Enter second string: ";
            cin >> str2;
        
            if (areAnagrams(str1, str2))
                cout << "\"" << str1 << "\" and \"" << str2 << "\" are anagrams." << endl;
            else
                cout << "\"" << str1 << "\" and \"" << str2 << "\" are not anagrams." << endl;
        
            return 0;
        }
Output:-
Enter first string: listen Enter second string: silent "listen" and "silent" are anagrams.

(c.) Python Code

def are_anagrams(str1, str2):
        # Remove spaces and convert to lowercase
        str1 = str1.replace(" ", "").lower()
        str2 = str2.replace(" ", "").lower()
        
        # Check if sorted characters of both strings are equal
        return sorted(str1) == sorted(str2)
    
    # Example usage
    str1 = "listen"
    str2 = "silent"
    
    if are_anagrams(str1, str2):
        print(f'"{str1}" and "{str2}" are anagrams.')
    else:
        print(f'"{str1}" and "{str2}" are not anagrams.')
Output:-
"listen" and "silent" are anagrams.

(d.) Java Code

import java.util.Arrays;

        public class AnagramCheck {
            // Function to check if two strings are anagrams
            static boolean areAnagrams(String str1, String str2) {
                // Remove spaces and convert to lowercase
                str1 = str1.replaceAll("\\s", "").toLowerCase();
                str2 = str2.replaceAll("\\s", "").toLowerCase();
        
                // Convert to character arrays and sort
                char[] arr1 = str1.toCharArray();
                char[] arr2 = str2.toCharArray();
                Arrays.sort(arr1);
                Arrays.sort(arr2);
        
                return Arrays.equals(arr1, arr2);
            }
        
            public static void main(String[] args) {
                String str1 = "listen";
                String str2 = "silent";
        
                if (areAnagrams(str1, str2)) {
                    System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are anagrams.");
                } else {
                    System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are not anagrams.");
                }
            }
        }
Output:-
"listen" and "silent" are anagrams.