Way2Java

Palindrome Delete Replace Reverse Java Example

Note: Palindrome Example is available in second page.

1. Deleting the Characters

To delete characters from StringBuffer, two methods exist – deleteCharAt() and delete().

Supported methods from String class

  1. StringBuffer deleteCharAt(int pos): Delete the character in the StringBuffer at the specified index number, pos.
  2. StringBuffer delete(int start, int end): Deletes all the characters in the StringBuffer existing in between start and end-1.
	
public class DeleteDemo
{
  public static void main(String args[])
  {					        // to delete one character
     StringBuffer buffer1 = new StringBuffer("Lord Almighty");
     buffer1.deleteCharAt(3);   		// deletes d
     System.out.println(buffer1);		// prints Lor Almighty

			// to delete a few characters
     StringBuffer buffer2 = new StringBuffer("UnitedStates");
     buffer2.delete(1, 11);   	                // deletes nitedState (1 to 9-1)
     System.out.println(buffer2);		// prints Us	
					// to delete all
     StringBuffer buffer3 = new StringBuffer("Himalayas");
     buffer3.delete(0, buffer3.length());   	// deletes all characters 
     System.out.println(buffer3);		// no output
  }
}
Output screen of DeleteDemo.java

buf1.deleteCharAt(4);

The method deleteCharAt(4) deletes only a single character present at 4th index number.

buf2.delete(2, 8);

The method delete(2, 8) deletes a group of characters that ranges from 2 to 8-1.

buf3.delete(0, buf3.length());

The above statement also deletes a group of characters but it is complete string because the first parameter is 0, the starting index of the string and buf3.length(), the complete length of string.

2. Replacing and Reversing Characters

We have seen earlier, deleting a single character and few characters. Now let us go for replacing a single character and few characters. For this two methods exist – setCharAt() and replace().

In this program, let us see how to reverse the contents of StringBuffer using reverse() method.

Supported methods from String class

  1. void setCharAt(int x, char ch): Replaces the existing character at the index number x with the character ch.
  2. StringBuffer replace(int start, int end, String str): Replaces all the characters existing from start to end-1 with string str.
  3. StringBuffer reverse(): Reverses all the characters in StringBuffer.
	
public class ReplaceAndReverse
{
   public static void main(String args[])
   {						// replacing one character
      StringBuffer buffer1 = new StringBuffer("planetarium");
      System.out.println(buffer1);		// planetarium
      buffer1.setCharAt(0, 'P');
      System.out.println(buffer1);              // Planetarium
						// replacing a group characters
      StringBuffer buffer2 = new StringBuffer("Lord Balaji");
      buffer2.replace(0, 4, "Great");	        // repalces 0, 1, 2, 3 (4-1) with Great
      System.out.println(buffer2);		// Great Balaji
  				        // reversing the characters
      StringBuffer buffer3 = new StringBuffer("INDIA");
      System.out.println(buffer3);		// INDIA
      buffer3.reverse();
      System.out.println(buffer3);		// AIDNI
   }
}
Output screen of ReplaceAndReverse.java
  1. buffer1.setCharAt(0, ‘P’) replaces the single character p existing in the index number 0 in buffer1 with P.
  2. buffer2.replace(0, 4, “Great”) replaces the characters existing from 0 to 3 (constituting Lord) with Great.
  3. buffer3.reverse() reverses the contents of buffer3. The affect comes in the original buffer itself.

Palindrome – Practical Example

A word that reads the same from start to end and end to start is known as palindrome. Observe, the following palindrome program written in Java is how much simpler than that of C/C++.

public class PalindromeTest
{
   public static void main(String args[])
   {					
      String s1 =  "RADAR";
      StringBuffer buffer1 = new StringBuffer(s1);
      StringBuffer buffer2 = buffer1.reverse();
      System.out.println(buffer2);		        // RADAR

      String s2 = buffer2.toString();
      if(s1.equals(s2))
           System.out.println("Palindrome");		// Palindrome
      else
           System.out.println("Not palindrome");		
     }
}

Output screen of PalindromeTest.java

buffer1.reverse() statement reverses the characters in the original StringBuffer buffer1 itself.

String s2 = buffer2.toString();

The equals() method requires two string objects to compare. For this reason, buffer2 is converted into string using toString() method of Object class. The above string conversion can be done as follows also by using the static method valueOf() of String class.

String s2 = String.valueOf(buffer2);