pgm

pgm

ANAGRAM program:

eg: "parliment"-"partial men" "software"-"swear oft"

write a java program that figures out whether one string in an anagram of another string. Program should ignore whitespace and punctuation.

View Answers

March 31, 2011 at 4:24 PM

import java.util.*;
public class AnagramExample {
    public static boolean checkAnagrams(String s1, String s2){
        String st1 = remove(s1);
        String st2 = remove(s2);
        st1 = st1.toLowerCase();
        st2 = st2.toLowerCase();
        st1 = sort(st1);
        st2 = sort(st2);
        return st1.equals(st2);
    }
    public static String remove(String st) {
        int i, len = st.length();
        StringBuilder builder = new StringBuilder(len);
        char ch;
        for (i = (len - 1); i >= 0; i--) {
            ch = st.charAt(i);
            if (Character.isLetter(ch)) {
                builder.append(ch);
            }
        }
        return builder.toString();
    }
    public static String sort(String st) {
        char arr[] = st.toCharArray();
        Arrays.sort(arr);
        return new String(arr);
    }
    public static void main(String[] args) {
        String string1 = "Parliament";
        String string2 = "partial men";
        System.out.println("    String 1: " + string1);
        System.out.println("    String 2: " + string2);
        System.out.println();

        if (checkAnagrams(string1, string2)) {
            System.out.println("Strings are anagrams!");
        } else {
            System.out.println("Strings are not anagrams!");
        }
       }
}









Related Tutorials/Questions & Answers:
pgm
ModuleNotFoundError: No module named 'pgm'
ModuleNotFoundError: No module named 'pgm'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'pgm' How to remove the ModuleNotFoundError: No module named 'pgm' error
Advertisements
ModuleNotFoundError: No module named 'broad_pgm_translator'
ModuleNotFoundError: No module named 'broad_pgm_translator'  Hi...: No module named 'broad_pgm_translator' How to remove the ModuleNotFoundError: No module named 'broad_pgm_translator' error? Thanks   Hi
ModuleNotFoundError: No module named 'broad_pgm_translator'
ModuleNotFoundError: No module named 'broad_pgm_translator'  Hi...: No module named 'broad_pgm_translator' How to remove the ModuleNotFoundError: No module named 'broad_pgm_translator' error? Thanks   Hi
simple pgm
Triangle pgm
java lab pgm - Framework
java help
java help  How to Open CSV Files in a Microsoft Excel Application Using Java Code with example pgm
what are different M.I.S reporting tools basically used?
what are different M.I.S reporting tools basically used?  write a java pgm using keyword super,static nd final
kedhar
kedhar  i'm getting the problem as error: cannot read: Print.java 1 error this problem occuring me when i'm trying to compile the pgm with javac classname.java please help me out this POPEstrong text
java program
java program  pgm in java that connects to a database and generates a report consisting of the courses in whom the total percentage of number of students failed is more than 80% of the total number of students who appeared
dynamic query
|| '(case when '||R.PORTFOLIO||'_TECH_ASS_C >= (select PGM_RQMT_DET_TARGET from DIM_MPE_SF_PGM_RQMT_DET where PGM_LVL_RQMT_SPEC_LEVEL =''ELITE SPECIALIST'' and PGM_LVL_RQMT_TECH='''||R.PORTFOLIO||''' and PGM_RQMT_DET
hai java - Java Beginners
hai java  what do u mean by unchecked exception and checked exception? and do any java pgm will run with out thread concept... and what si thread...  Hi Friend, Please visit the following link: http
how to insert, retrieve data from,to db(code)....
validations for username, password using javascript,hw to write that,n in wch pgm i ve to write(i mean, in login jsp pgm itself r in new jsp pgm) if it is in login pgm whr it shud b coded(before username,pasword fields code r after
Java code
Java code  hi, please suggest me how to extract table name and find the no of select statement and insert statement from a file and dispaly the result of SELECT and INSERT tablewise i.e. abc_table SELECT 4 pgm_table
java question
java question  how to create date and time based pgm in java... in db table if date and time crossed automatically update in the db and inform to login user as pop ups   http://www.roseindia.net/jsp/loginstatus.shtml
Java String manipulation
Java String manipulation  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon
Java String
Java String  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon as possible Thank you
java fundamental question related to string
??); } } Output? a) the pgm will throw ClassCastException b) compilation
java - Java Beginners
java  how to create the window in java pgm?  in java we create frames Frame f=new Frame(); f.setVisible(true); f.setSize(300,300);  Hi Friend, Try it: import javax.swing.*; import java.awt.
how to insert, retrieve data from,to db(code)....
for username, password using javascript,hw to write that,n in wch pgm i ve to write(i mean, in login jsp pgm itself r in new jsp pgm) if it is in login pgm whr it shud b
Java Applet
code snippet of my pgm: public void mouseClicked(MouseEvent m) { x
Java Applet
code snippet of my pgm: public void mouseClicked(MouseEvent m) { x
single selection of chechbox among multiple selection when in a loop.
single selection of chechbox among multiple selection when in a loop.  i am using cmd prompt to run and execute the java pgm. here i need your help that when in a loop i trying to choose single checkbox among multiple checkboxes
Fresher-Trainee
Fresher-Trainee  the below pgm has an exception during run time, and has no errors during compilation , pls help. wat to do? class Pyramid { (adsbygoogle = window.adsbygoogle || []).push({}); public static
jdbc - JDBC
jdbc  hi to every one......... i'm doing project on contact management so i need how i can connect the pgm to database by using jdbc...? if u replyed its very useful for me...  Hi, Please read JDBC tutorial
Program to find no of metrics in a file ----- Java code
Statement Occurance Pgm_table SELECT 4 Abc_table
user defined subclass of throwable class - Java Beginners
user defined subclass of throwable class  Dear Sir,pls help me to write this pgm: Program to illustrate the use of user defined subclass of throwable class  Hi Friend, Try the following: 1) public class
how to calculate max and min in the loop - Java Beginners
;here...a is an array....the data type is String.... This pgm takes values
compilation error in java - Java Beginners
compilation error in java  Here is the pgm usinf if-else statement /* To find the grade of the student */ class grade() { public static void main(String [ ] args) { int testscore=76; char grade; if (testscore>=79
java features - Java Beginners
java features  Why java is called as neutral language  hai frnd.. JAVA is not neutral language...but ARCHITECTURE-NEUTRAL.. 1 of the main issue of the programmers is that no guarantee exists that if u write a pgm
Java String operations
4 pgm_table INSERT 5 Please tell me how to do the coding
java
java  write a java pgm to illustrate the keywords super,static and final   super Keyword: The keyword super is used to refer to the parent class instance of the current class. class Base{ protected int num; public Base
JAVA THREAD - Java Beginners
JAVA THREAD  hii i wrote a pgm to print the numbers from 0 to 9 in 2 threads. but it couldn't work pls help me int it. the code is given below: class kj implements Runnable { public static void main(String ar[])throws
Jfreechart - Java Beginners
folder. And I set classpath for both the jar files. Den when I run the pgm am
in registration page,hw the details wil b stored in db,n retrieved.....
in db(do v need to write select query in d same pgm)? and here is my code tthe
problem in jsp using ms-access
problem in jsp using ms-access  after starting server(tomcat) wen v run the pgm,we are just getting a blank page.I guess we ve a pblm.. in the follwing line of code.. Connection con-DriverManager.getConnection
hi i m new to hibernate..plzzzzzzzzzzzzzz help me.... - Hibernate
hi i m new to hibernate..plzzzzzzzzzzzzzz help me....  hi friends i m new to hibernate..& i m stuck in one positon in my pgm.. its a simple select query which i wan to run...with oracle thin database connection
using class and methods - Java Beginners
using class and methods  Sir,Plz help me to write this below pgm. here is the question: Assume a table for annual examination results for 10 students. write a program to read the data and determine the following: a) Total
Problem in Servlet - Servlet Interview Questions
. then some exception ..Servlet Exception: empContrller is not a servlet pgm

Ads