Showing posts with label Java Program on Strings. Show all posts
Showing posts with label Java Program on Strings. Show all posts

Wednesday, February 8, 2012

java program To print a String in reverse order using recursion

// to print a string in reverse order using recursion
import java.io.*;
class reverseString
{
public static void main(String args[])throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");

String s=br.readLine();
reverse(s, s.length());
}

public static void reverse(String S, int a)throws IOException
{
System.out.print(S.charAt(--a));
if(a>0)
reverse(S,a);
}
}

Saturday, February 6, 2010

java program To find whether two strings are anagrams

import java.io.*;
class anagrams
{
public void main(String args[])throws IOException
{
String s1,s2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Anagram Tester! Enter the strings:");
System.out.print("String 1: ");
s1=br.readLine();
System.out.print("String 2: ");
s2=br.readLine();
s1=removewhitespace(s1);
s2=removewhitespace(s2);
s1=s1.toLowerCase();
s2=s2.toLowerCase();

if(s1.length()!=s2.length())System.out.print("Not anagrams(no. of letters not same)!");
else
{
for(int i=0;i{
for(int j=0;j{
if(s1.charAt(i)==s2.charAt(j)){s2=delete(s2,j);break;}
}
}
if(s2.equals("")) System.out.print("The strings are anagrams!");
else System.out.print("Not anagrams "+s2);
}
}

public String removewhitespace(String s)
{
for(int i=0;i{
if(s.charAt(i)==' ')s=delete(s,i);
}
return s;
}

public String delete(String s,int ind)
{
String str=s.substring(ind+1,s.length());
s=s.substring(0,ind);
s=s+str;
return s;
}
}