Automation Using Selenium Webdriver

Thursday, 1 December 2016

Java Interview programs on Strings

1.Reverse a String Without using String API?


package com.java;

public class ReverseString {

public static void main(String[] args) {

String str="Hello world";
String revstring="";

for(int i=str.length()-1;i>=0;--i){
revstring +=str.charAt(i);
}

System.out.println(revstring);
}
}


Program:

output: dlrow olleH.

2)Sorting the String without using String API?


package com.java;

public class SortString {

 public static void main(String[] args) {

  String original = "edcba";
 int j=0;
   char temp=0;

     char[] chars = original.toCharArray();
     for (int i = 0; i <chars.length; i++) {
         for ( j = 0; j < chars.length; j++) {
         if(chars[j]>chars[i]){
             temp=chars[i];
             chars[i]=chars[j];
              chars[j]=temp;
          }
     }
  }

    for(int k=0;k<chars.length;k++){
    System.out.println(chars[k]);
   }

 }
}

program:

output:abcde.

3.Sort the String with using String API?

program:
package com.java;

 public class SortString {

 public static void main(String[] args) {
   String original = "edcba";

   char[] chars = original.toCharArray();
   Arrays.sort(chars);

    String sorted = new String(chars);
     System.out.println(sorted);

}
}




OutPut:abcde

4.Check String is palindrome or not?

program:

Solution #1:

package com.java;

public class PalindromeDemo{

public static void main(String[] args) {

String str="MADAM";
String revstring="";

for(int i=str.length()-1;i>=0;--i){
revstring +=str.charAt(i);
}

System.out.println(revstring);

if(revstring.equalsIgnoreCase(str)){
System.out.println("The string is Palindrome");
}
else{
System.out.println("Not Palindrome");
}

}

}



  Output:

The string is Palindrome

Solution #2:

package com.java;

  import java.util.Scanner;

 public class Palindrome {

public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

 System.out.println("Enter a string");
 String str=in.nextLine();

StringBuffer strone=new StringBuffer(str);
 StringBuffer strtwo=new StringBuffer(strone);

  strone.reverse();

System.out.println("Orginal String ="+strtwo);
System.out.println("After Reverse ="+strone);

if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
   System.out.println("Result:Palindrome");
   else
    System.out.println("Result:Not Palindrome");

   }

}
 Output:

Enter a string
MOOM
Orginal String =MOOM
After Reverse =MOOM

Result:Palindrome

5.Program to Check given number is palindrome or not?

Program:

 package com.java;

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args)
{

System.out.println("Please Enter a number : ");
      int givennumber = new Scanner(System.in).nextInt();
      int number=givennumber;
       int reverse=0;
        while (number != 0) {
           int remainder = number % 10;
            reverse = reverse * 10 + remainder;
            number = number / 10;
        }        
if(givennumber == reverse)
   System.out.println("Result:Palindrome");
    else
    System.out.println("Result:Not Palindrome");
    }

}

 Output:

Please Enter a number :
535
Result:Palindrome.

No comments:

Post a Comment