Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


nisha
pgm
1 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


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.


Advertisement
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
Advertisements
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
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 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
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
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
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
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
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 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.
Program to find no of metrics in a file ----- Java code
Statement Occurance Pgm_table SELECT 4 Abc_table
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 { public static void main(String[] args) { int x = 9; for (int i
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
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
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 String operations
4 pgm_table INSERT 5 Please tell me how to do the coding
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
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
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
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
 

 

 

DMCA.com