The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Interview programs on Strings

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Interview programs on Strings Posted: Apr 25, 2015 1:36 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Interview programs on Strings
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement



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.

6.

Read: Java Interview programs on Strings

Topic: JDBC Connection Strings for Popular RDBMS Previous Topic   Next Topic Topic: Unique approach to observer/observable pattern in Ceylon

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use