1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
lutik1710 [3]
4 years ago
8

Two strings are anagrams if they are written using the same exact letters. Write a method to check if given two strings are anag

rams 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

Computers and Technology
2 answers:
adelina 88 [10]4 years ago
7 0

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

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.

Andre45 [30]4 years ago
4 0

Answer:

Check the explanation

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.

You might be interested in
The ip address 221.1.10.53 /24 is a what type of address?
jekas [21]
It is a class C address.  Class C covers all addresses in the range 192.0.0.0 to 223.0.0.0.  The /24 represents the number of network bits in the address so we can work out that the subnet mask of this address would be 255.255.255.0.
8 0
3 years ago
A(n) ____________ is a private data network that creates secure connections over regular internet lines.
Setler79 [48]
VPN is the correct answer
5 0
4 years ago
Favorite video games list atleast 5<br><br> favorite to least
Aneli [31]

Answer:

Rob lox, Sub nautica, Phasmo phobia, Albion Online,  fnf.

Explanation:

6 0
3 years ago
Question 8 of 10
kramer

Answer:

C. The energy used to create and maintain technology

#Carry on learning po

3 0
3 years ago
What is the oop in c++ ?
Juli2301 [7.4K]

Answer:

<h2>OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. ... OOP is faster and easier to execute.</h2>

<h2>HOPE IT HELPS </h2>

5 0
3 years ago
Other questions:
  • What kind of voltage do solar cells generate? Solar cells produce ______ voltage which is not usable by most household appliance
    15·1 answer
  • What does the hard disk drive do? It stores all of the information on a computer. It controls a computer’s operating system. It
    12·2 answers
  • )in the link based implementation of the ADT sorted list what is the worst case time efficiency of the remove method?
    7·1 answer
  • Assume that the following variables have been defined in a program: int x = 10; int y = 20; int z = 30; Write a cout statement t
    11·1 answer
  • What answer best explains why improper netiquette is considered dangerous? Individuals who violate user policies are often charg
    14·2 answers
  • While your hands are on home row, your left hand rests lightly on _____.
    13·2 answers
  • To use   ( 2 complement ) answer it (101101)2 – (1100)2 =   (                    )2
    12·1 answer
  • Submit your business presentation that clearly compares and contrasts three different cell phone service plans..
    14·2 answers
  • In two-dimensional arrays, the _____________ is always listed second.
    6·1 answer
  • Pls need this asap
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!