» » Java Interview programs on Strings


1.Reverse a String Without using String API?

  1. package com.instaceofjava;
  2.  
  3. public class ReverseString {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="Hello world";
  8. String revstring="";
  9.  
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  13.  
  14. System.out.println(revstring);
  15. }
  16. }


Program:

output: dlrow olleH.

2)Sorting the String without using String API?


  1. package com.Instanceofjava;
  2.  
  3. public class SortString {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.   String original = "edcba";
  8.  int j=0;
  9.    char temp=0;
  10.  
  11.      char[] chars = original.toCharArray();
  12.      for (int i = 0; i <chars.length; i++) {
  13.          for ( j = 0; j < chars.length; j++) {
  14.          if(chars[j]>chars[i]){
  15.              temp=chars[i];
  16.              chars[i]=chars[j];
  17.               chars[j]=temp;
  18.           }
  19.      }
  20.   }
  21.  
  22.     for(int k=0;k<chars.length;k++){
  23.     System.out.println(chars[k]);
  24.    }
  25.  
  26.  }
  27. }

program:

output:abcde.

3.Sort the String with using String API?

program:
  1. package com.instanceofjava;
  2.  
  3.  public class SortString {
  4.  
  5.  public static void main(String[] args) {
  6.    String original = "edcba";
  7.  
  8.    char[] chars = original.toCharArray();
  9.    Arrays.sort(chars);
  10.  
  11.     String sorted = new String(chars);
  12.      System.out.println(sorted);
  13.  
  14. }
  15. }




OutPut:abcde

4.Check String is palindrome or not?

program:

Solution #1:

  1. package com.instaceofjava; 
  2.  
  3. public class PalindromeDemo{ 
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="MADAM";
  8. String revstring="";
  9.  
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  13.  
  14. System.out.println(revstring);
  15.  
  16. if(revstring.equalsIgnoreCase(str)){
  17. System.out.println("The string is Palindrome");
  18. }
  19. else{
  20. System.out.println("Not Palindrome");
  21. }
  22.  
  23. }
  24.  
  25. }



  Output:

The string is Palindrome

Solution #2:

  1. package com.instaceofjava;
  2.  
  3.   import java.util.Scanner;
  4.  
  5.  public class Palindrome {
  6.  
  7. public static void main(String[] args)
  8. {
  9.  
  10. Scanner in = new Scanner(System.in);
  11.  
  12.  System.out.println("Enter a string");
  13.  String str=in.nextLine();
  14.  
  15. StringBuffer strone=new StringBuffer(str);
  16.  StringBuffer strtwo=new StringBuffer(strone);
  17.  
  18.   strone.reverse();
  19.  
  20. System.out.println("Orginal String ="+strtwo);
  21. System.out.println("After Reverse ="+strone);
  22.  
  23. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  24.    System.out.println("Result:Palindrome");
  25.    else
  26.     System.out.println("Result:Not Palindrome");
  27.  
  28.    }
  29.  
  30. }




 Output:

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

Result:Palindrome

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

Program:

  1.  package com.instaceofjava; 
  2.  
  3. import java.util.Scanner; 
  4.  
  5. public class Palindrome {
  6.  
  7. public static void main(String[] args)
  8. {
  9.  
  10. System.out.println("Please Enter a number : ");
  11.       int givennumber = new Scanner(System.in).nextInt();
  12.       int number=givennumber;
  13.        int reverse=0;
  14.         while (number != 0) {
  15.            int remainder = number % 10;
  16.             reverse = reverse * 10 + remainder;
  17.             number = number / 10;
  18.         }           
  19. if(givennumber == reverse)
  20.    System.out.println("Result:Palindrome");
  21.     else
  22.     System.out.println("Result:Not Palindrome");
  23.     }
  24.  
  25. }

 Output:

Please Enter a number :
535
Result:Palindrome.

 Also Read:


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu