Archive | November, 2012

Permutation of a sting (taking input from user )

5 Nov
import java.util.*;
class permutation {
    public static void main(String args[]){
       Scanner input=new Scanner(System.in);
String st =input.nextLine();
    permutation ("",st);
}
 
public static void permutation(String prefix,String str){
    if(str.length() <= 1)
    System.out.println(prefix + str);
    else
    for(int i=0;i < str.length();i++){
        permutation(prefix+str.charAt(i),str.substring(0,i)+str.substring(i+1));
    }
}
}

Recombination of a String

3 Nov
import java.util.*;
class MainClass{
public static void main(String args[]){
    Scanner input=new Scanner(System.in);
String s =input.nextLine();
  if(s!= null && s.length()!=0)
   RecCombine("",s);
 }
 
 static void RecCombine(String prefix,String rest){
  if(rest.length() == 0)
   System.out.print(prefix + " ");
  else{
   RecCombine(prefix + rest.charAt(0),rest.substring(1));
   RecCombine(prefix,rest.substring(1));
   
  }
 }
}

Permutation of a string

3 Nov

class MainClass {
public static void main(String args[]) {
permuteString(“”, “Devlina”);
}

public static void permuteString(String beginningString, String endingString) {
if (endingString.length() <= 1)
System.out.println(beginningString + endingString);
else
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);

permuteString(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
}

Reverse of a string

3 Nov

import java.util.*;
class ReverseWords{
public static void main(String args[]){
System.out.print(“Enter the string: “);
Scanner input=new Scanner(System.in);
String str=input.nextLine();
StringBuffer buffer = new StringBuffer(str);
StringTokenizer st = new StringTokenizer(buffer.reverse().toString(), ” “);
System.out.print(“Reversed Words: “);
while(st.hasMoreTokens()){
StringBuffer sb= new StringBuffer(st.nextToken());
System.out.print(” “+sb.reverse());
}
}
}