import java.util.Arrays;
public class CheckAnagram {
public static void main(String[] args) {
String firstString = "Army";
String secondString = "mary";
System.out.println("Check if the firstString and secondString is anagram of each other: "+ isAnagram(firstString, secondString));
}
public static boolean isAnagram(String firstString, String secondString){
char[] firstStringCharArray = firstString.toLowerCase().toCharArray();
char[] secondStringCharArray = secondString.toLowerCase().toCharArray();
Arrays.sort(firstStringCharArray);
Arrays.sort(secondStringCharArray);
return Arrays.equals(firstStringCharArray, secondStringCharArray);
}
}
public class CheckAnagram {
public static void main(String[] args) {
String firstString = "Army";
String secondString = "mary";
System.out.println("Check if the firstString and secondString is anagram of each other: "+ isAnagram(firstString, secondString));
}
public static boolean isAnagram(String firstString, String secondString){
char[] firstStringCharArray = firstString.toLowerCase().toCharArray();
char[] secondStringCharArray = secondString.toLowerCase().toCharArray();
Arrays.sort(firstStringCharArray);
Arrays.sort(secondStringCharArray);
return Arrays.equals(firstStringCharArray, secondStringCharArray);
}
}
public class Main
{
public static void main(String[] args)
{
String inputString = "malayalam";
int length = inputString.length();
int i, begin, end, middle;
begin = 0;
end = length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++)
{
if (inputString.charAt(begin) == inputString.charAt(end))
{
begin++;
end--;
}
else
{
System.out.println("Not a palindrome.");
break;
}
}
if (i == middle + 1)
{
System.out.println("Palindrome.");
}
}
}
{
public static void main(String[] args)
{
String inputString = "malayalam";
int length = inputString.length();
int i, begin, end, middle;
begin = 0;
end = length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++)
{
if (inputString.charAt(begin) == inputString.charAt(end))
{
begin++;
end--;
}
else
{
System.out.println("Not a palindrome.");
break;
}
}
if (i == middle + 1)
{
System.out.println("Palindrome.");
}
}
}
import java.util.Scanner;
class ReverseWords
{
static void reverseWords(String str)
{
String[] arr = str.split("\\s");
for (int i = arr.length-1; i >= 0; i--)
{
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args)
{
String str = "i like this program very much";
reverseWords(str);
}
}
class ReverseWords
{
static void reverseWords(String str)
{
String[] arr = str.split("\\s");
for (int i = arr.length-1; i >= 0; i--)
{
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args)
{
String str = "i like this program very much";
reverseWords(str);
}
}
public class RemoveDuplicates {
public static void main(String[] args)
{
String str = "Sathya";
int l = str.length();
int k = 0;
char[] temp = new char[l];
boolean[] hit = new boolean[256];
for (int i = 0; i < l; i++) {
char ch = str.charAt(i);
if (!hit[ch]) {
hit[ch] = true;
temp[k++] = ch;
}
}
String res = new String(temp, 0, k);
System.out.println(res);
}
}
public static void main(String[] args)
{
String str = "Sathya";
int l = str.length();
int k = 0;
char[] temp = new char[l];
boolean[] hit = new boolean[256];
for (int i = 0; i < l; i++) {
char ch = str.charAt(i);
if (!hit[ch]) {
hit[ch] = true;
temp[k++] = ch;
}
}
String res = new String(temp, 0, k);
System.out.println(res);
}
}
//Write a java program accept date of birth from scanner and check eligible for vote ?
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter date of birth in format dd/mm/yyyy : ");
String dob = sc.nextLine();
String[] dobArray = dob.split("/");
int day = Integer.parseInt(dobArray[0]);
int month = Integer.parseInt(dobArray[1]);
int year = Integer.parseInt(dobArray[2]);
if(year < 1950)
{
System.out.println("You are not eligible for vote.");
}
else if(year == 1950 && month < 1)
{
System.out.println("You are not eligible for vote.");
}
else if(year == 1950 && month == 1 && day <18)
{
System.out.println("You are not eligible for vote.");
}
else
{
System.out.println("You are eligible for vote.");
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter date of birth in format dd/mm/yyyy : ");
String dob = sc.nextLine();
String[] dobArray = dob.split("/");
int day = Integer.parseInt(dobArray[0]);
int month = Integer.parseInt(dobArray[1]);
int year = Integer.parseInt(dobArray[2]);
if(year < 1950)
{
System.out.println("You are not eligible for vote.");
}
else if(year == 1950 && month < 1)
{
System.out.println("You are not eligible for vote.");
}
else if(year == 1950 && month == 1 && day <18)
{
System.out.println("You are not eligible for vote.");
}
else
{
System.out.println("You are eligible for vote.");
}
}
}