0% found this document useful (0 votes)
9 views2 pages

Rahul Java

The Java program defines a class 'Rahul' with methods to count vowels in a string and check if two strings are anagrams. It converts the input strings to lowercase, checks their lengths, and compares their sorted character arrays to determine if they are anagrams. The main method reads two strings from user input and calls the Anagrams method to display the result.

Uploaded by

rahulsuthrapu616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Rahul Java

The Java program defines a class 'Rahul' with methods to count vowels in a string and check if two strings are anagrams. It converts the input strings to lowercase, checks their lengths, and compares their sorted character arrays to determine if they are anagrams. The main method reads two strings from user input and calls the Anagrams method to display the result.

Uploaded by

rahulsuthrapu616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;
import java.util.Arrays;

public class Rahul {

public static int countVowels(String str){


int count=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch=='a'|ch=='e'|ch=='i'|ch=='o'|ch=='u'){
count++;
}
}
return count;
}

public static void Anagrams(String str1,String str2){


//input Strings s1 and s2

//convert strings to lowercase


str1=str1.toLowerCase();
str2=str2.toLowerCase();

//First check if lengths are equal


if(str1.length()==str2.length()){
//convert Strings to char Arrays
char[] str1charArray=str1.toCharArray();
char[] str2charArray=str2.toCharArray();

//sort the char Arrays


Arrays.sort(str1charArray);
Arrays.sort(str2charArray);

//if the sorted are same or identical, Then Strings are


anagrams

boolean result=Arrays.equals(str1charArray,str2charArray);
if(result){
System.out.println(str1+" "+str2+"are ANAGRAMS of
eachother");
}else{
System.out.println(str1+" "+str2+"are NOT ANAGRAMS of
eachother");
}

}else{
//case when lengths are not equal
System.out.println(str1+" "+str2+"are NOT ANAGRAMS of
eachother");
}

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
String str1=new String();
str1=sc.nextLine();
String str2=new String();
str2=sc.nextLine();
Anagrams(str1, str2);

You might also like