answer.
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]
2 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]2 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]2 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
A smart refrigerator can use _____ to detect when you are running low on milk, and then send a reminder to you on a wireless net
Brilliant_brown [7]

Answer:

IoTs

Explanation:

4 0
2 years ago
18. Which type of briefing is delivered to individual resources or crews who are assigned to operational tasks and/or work at or
GrogVix [38]

Answer: Field-level briefing

Explanation: Field-level briefing is the brief description that is provided to the crew members or operation task holders.This briefing contains the information about the work and operation that are to be followed by the workers on the site .

Field level briefing is sort of instructions to individual or group of people who will be working with their assigned duties on the incident site.

8 0
2 years ago
Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input.
Neko [114]

Answer:

In C++:

#include <iostream>

#include <string>

#include <sstream>

using namespace std;

int main(){

   string lname, fname,stringnum;    int num; string login, pwd;

   cout<<"Last Name: ";    cin>>lname;

   cout<<"First Name: ";    cin>>fname;

   cout<<"Four-digit integer: ";    cin>>num;

   stringnum = to_string(num).substr(0,4);

   stringstream geek(stringnum);    geek>>num;

   num = num%100;

   pwd = to_string(num);

   if(lname.length()<5){ login = lname+fname.substr(0,1);    }

   else{ login = lname.substr(0,5)+fname.substr(0,1);    }

   cout<<"Login Name: "<<login<<endl;

   cout<<"Password: "<<pwd<<endl;

   return 0;

}

Explanation:

This declares all necessary variables

   string lname, fname,stringnum;    int num;     string login, pwd;

This prompts the user for last name

   cout<<"Last Name: ";    cin>>lname;

This prompts the user for first name

   cout<<"First Name: ";    cin>>fname;

This prompts the user for a four digit integer

   cout<<"Four-digit integer: ";    cin>>num;

This converts the input number to string and gets only the first four integer

   stringnum = to_string(num).substr(0,4);

This converts the string back to an integer

   stringstream geek(stringnum);    geek>>num;

This gets the last two of the four digit integer

   num = num%100;

This gets the password of the user

  pwd = to_string(num);

This gets the login name of the user.

This is executed if the length of the first name is less than 5

<em>    if(lname.length()<5){ login = lname+fname.substr(0,1);    }</em>

This is executed if otherwise

<em>    else{ login = lname.substr(0,5)+fname.substr(0,1);    }</em>

This prints the login name

   cout<<"Login Name: "<<login<<endl;

This prints the password

   cout<<"Password: "<<pwd<<endl;

3 0
2 years ago
Which technology can be used to protect the privacy rights of individuals and simultaneously allow organizations to analyze data
iren [92.7K]

Answer:

De-identification or data anonymization.

Explanation:

Privacy rights are fundamental right of individuals to privatise all personal information, when creating an account.

The de-identification and data anonymization technology is provided by the organisation to user, to prevent their information to be viewed by others. It commonly used in cloud computing, communication, internet, multimedia etc. Reidentification is the reversing of the de-identification effect on personal data.

4 0
2 years ago
Braking on __________ may cause a loss of control.
kodGreya [7K]

Answer:Wet roads

Explanation:

7 0
2 years ago
Read 2 more answers
Other questions:
  • Ben's team is working on an English project. The members want to see everyone's progress on their part of the project. What tech
    7·2 answers
  • A vehicle fails an HC emission test at idle and 2,500 rpm, and the engine has an acceleration stumble. The heated oxygen sensor
    15·1 answer
  • Give a recursive (or non-recursive) algorithm to compute the product of two positive integers, m and n, using only addition and
    8·1 answer
  • In a fantasy world, your character must face hordes of demons. Each demon is vulnerable to a type of magical spell. This weaknes
    6·2 answers
  • Insert an IF function in cell F5 to calculate the total due. If the customer has chosen home delivery, there is an additional de
    8·2 answers
  • You have configured your firewall to authenticate a group of 100 users who are in your company. You set up the database of users
    14·1 answer
  • The function below takes a single string parameter: sentence. Complete the function to return everything but the middle 10 chara
    15·1 answer
  • You resurrected an old worksheet. It appears to contain most of the information that you need, but not all of it. Which step sho
    5·1 answer
  • When performing actions between your computer and one that is infected with a virus, which of the following offers No risk of yo
    12·1 answer
  • Write a static generic method PairUtil.minmax that computes the minimum and maximum elements of an array of type T and returns a
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!