102k views
2 votes
Two strings are anagrams if they are written using the same exact letters. Write a method to check if given two strings are anagrams or not. You have to ignore the case and space characters. Write a test program for that prompts the user to input two strings and invokes this method. Some example runs are: Enter the first string: abbacba Enter the second string: abcabba abbacba and abcabba are anagrams Enter the first string: banana Enter the second string: cabana banana and cabana are NOT anagrams Enter the first string: Eleven plus two Enter the second string: Twelve plus one Eleven plus two and Twelve plus one are anagrams

User Qdelettre
by
4.7k points

2 Answers

1 vote

Answer:

I am writing a Python program:

string1=input("Enter first string:") #prompts user to enter first string

string2=input("Enter second string:") #prompts user to enter 2nd string

if(sorted(string1)==sorted(string2)): #to check if two strings are anagrams

print(string1,"and", string2,"are anagrams")

# display above message if the above if condition is true and two string are #anagrams

else: #if the condition is not true

print(string1,"and", string2,"are not anagrams")

# display the above message if both string are not anagrams

Step-by-step explanation:

I will explain the code line by line

  • In the first statement the program asks the user to enter the value of the first string and string1 holds this input string.
  • In the second statement the program asks the user to enter the value of the second string and string2 holds this input string.
  • if(sorted(string1)==sorted(string2)) first sorts both the strings and then compares the sorted string. So if condition checks whether these sorted strings are equal or not. If they are equal this means that the two strings string1 and string2 are anagrams otherwise they are not anagrams.
  • sorted() function is used here to return the sorted list of the string1 and string2.
  • Lets take an example to explain the above code
  • Suppose the value of string1 is abbacba and value of string2 is abcabba
  • Now the sorted(string1) method will sort the value of string1 like this: aaabbbc.
  • The sorted(string2) method will sort the string2 like this: aaabbbc
  • Now according to the if condition these two sorted strings are compared to check if they are equal.
  • aaabbbc=aaabbbc so these are equal which means string1 and string2 are anagrams.
  • So the messages displayed is: abbacba and abcabba are anagrams .
  • The program along with the output is attached.
Two strings are anagrams if they are written using the same exact letters. Write a-example-1
User Prismaticorb
by
4.4k points
2 votes

Answer:

Check the explanation

Step-by-step explanation:

import java.util.Scanner;

public class Anagrams {

public static int count(String s, char ch) {

int c = 0;

for (int i = 0; i < s.length(); ++i) {

if (s.charAt(i) == ch) {

c++;

}

}

return c;

}

public static boolean isAnagram(String s1, String s2) {

char ch;

s1 = s1.toLowerCase();

s2 = s2.toLowerCase();

for (int i = 0; i < s1.length(); ++i) {

ch = s1.charAt(i);

if (ch != ' ' && count(s1, ch) != count(s2, ch)) {

return false;

}

}

for (int i = 0; i < s2.length(); ++i) {

ch = s2.charAt(i);

if (ch != ' ' && count(s1, ch) != count(s2, ch)){

return false;

}

}

return true;

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the first string: ");

String s1 = in.nextLine();

System.out.print("Enter the second string: ");

String s2 = in.nextLine();

if (isAnagram(s1, s2)) {

System.out.println(s1 + " and " + s2 + " are anagrams");

} else {

System.out.println(s1 + " and " + s2 + " are NOT anagrams");

}

}

}

Kindly check the output image below.

Two strings are anagrams if they are written using the same exact letters. Write a-example-1
Two strings are anagrams if they are written using the same exact letters. Write a-example-2
User Machinarius
by
4.7k points