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
How do I learn coding? Python
pickupchik [31]

first learn the basics about the computer than just slowing move into it you feel?

5 0
3 years ago
Read 2 more answers
Given that note, write a program which tells us the major scale starting at that note, using the pattern above. In the main bloc
White raven [17]

<u>Solution and Explanation:</u>

the_notes = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'] #original list

def get_scale(choice): #function to get the major diatonic scale

steps=[1,1,.5,1,1,1,.5] #separate list for steps

size=None

for i in range(0,12): #loop to find out the index of the note entered

if(the_notes[i]==choice):

size =i

break

new_notes=the_notes[size:] + the_notes[:size]

#the above statement rotate the list so that the entered note can become the first note in the new list(i.e new notes)

index =0 #

scale="" #a string to store the major diatonic scale

for i in range(0,7):

scale+=new_notes[index]+" "

if(steps[i]==1):

index = index+2

else:

index=index+1

return scale

#main block

while(1):

choice=input("What scale do you want or Q to quit? ")

if(choice=='Q'):

break

elif(choice in the_notes):

print(get_scale(choice))

else:

print("That is not a note")

5 0
3 years ago
Which of the following is one way a corporation can raise money?
Vladimir [108]
Selling stock on the stock market
4 0
4 years ago
A Floppy Disk is an example of:
lisov135 [29]

Answer:

B I think-

Explanation:

Its either Primary or Secondary :) pretty sure its secondary tho

6 0
3 years ago
Nina aspires to be a digital media specialist. What should Nana be familiar with in order to pursue this career?
brilliants [131]

computer science? maybe im not sure?

4 0
3 years ago
Read 2 more answers
Other questions:
  • What do you believe are the advantages of a clean install over an upgrade? What additional choices do you think are important to
    9·1 answer
  • What is the name of the table tools contextual tab that appears once you insert a table?
    8·2 answers
  • The c++ operator _______________ is used to destroy dynamic variables.
    5·1 answer
  • You could face mandatory revocation of your license as a result of __________ A. lying about ownership of your vehicle. B. drivi
    14·1 answer
  • Anyone willing to help with Excel?!!
    15·1 answer
  • A _______ is a computer running software that allows it to share resources over a network. it may share programs or data. select
    7·1 answer
  • Suppose you define a java class as follows: public class test { } in order to compile this program, the source code should be st
    14·1 answer
  • In molecular biology the "alphabet" of genes consists of four chemicals (called nucleotides) represented by the letters A C G T.
    12·2 answers
  • What is one reason to create a study check list​
    7·1 answer
  • the penalties for ignoring the requirements for protecting classified information when using social networking services are ____
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!