Anagram String

Anagram String

🧩 What is an Anagram?

Two strings are called Anagrams if they contain the exact same characters, just arranged in a different order. Think of it like a word puzzle where you rearrange letters to form a new word.

Examples:

  • LISTEN ↔️ SILENT
  • HEART ↔️ EARTH
  • RACE ↔️ CARE


📝 Understanding the Code


The program starts by setting up a Scanner to capture user input. It asks for two words and immediately applies the .toUpperCase() method to both. This is a crucial step because computers see 'A' and 'a' as completely different characters; converting everything to uppercase ensures the program only cares about the letter itself, not the casing.


Next, the program calculates the length of both words. This is where your new logic shines: it uses an if(l1 == l2) statement to check if the words are the same length. If they aren't, the program immediately jumps to the end and prints "Invalid Entry!" This "early exit" saves processing power and makes the logic more robust.


If the lengths match, the program enters a nested loop system. The outer loop selects one letter at a time from the first word using .charAt(i). While holding that letter, the inner loop runs through every letter of the second word (.charAt(j)) to find a match. Every time a match is found, the count variable increases by one.


In the final step, the program checks if the total count of matches equals the length of the word. If it does, it confirms the words are an Anagram. If the count doesn't match—which might happen if the letters are different—it let's the user know they are not anagrams.


The Code:

// Written By @Varnit_Baiswar 
import java.util.Scanner;
public class Anagram_String
{
  public static void main(){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a Word: ");
    String str1 = in.next().toUpperCase();
    System.out.println("Enter another Word: ");
    String str2 = in.next().toUpperCase();
    int l1 = str1.length();
    int l2 = str2.length();
    char c,ch;
    int count = 0;
    if(l1 == l2){  // if Length of both the string is same, proceed with the logic 
    for(int i = 0; i < l1; i++){
      c = str1.charAt(i);
      for(int j = 0; j < l2; j++){ // comparing Str1 with Str2
        ch = str2.charAt(j);
        if(c == ch){
          count++;
        }
      }
    }
    if( count == l1){
      System.out.println("The String is an Anagram String");
    }
    else{
      System.out.println("The String is not an Anagram String");

    }
    }
    else{
      System.out.println("Invalid Entry!");  // if length of both the strings is not same
    }
   }
  }



🔍 Key Methods Explained

  • toUpperCase(): This String method converts all lowercase letters in a string to uppercase. It standardizes the input so that the comparison is "case-insensitive."
  • length(): This method returns an integer representing the number of characters in the string. It is used here both as a preliminary check for equality and to set the limits for the loops.
  • charAt(index): This method acts like a surgical tool, allowing the program to extract a single character from a specific position (index) within the string for comparison.


Extra!

This "character-counting" logic is a fantastic way to practice nested loops! Just remember that in a professional setting, you might also handle duplicate letters by "sorting" the strings or using a frequency map to ensure every letter is matched only once.