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
  }
}
Palindrome
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
   }
}
Palindrome
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.

2 thoughts on “Palindrome Delete Replace Reverse Java Example”

  1. class ReplaceAndReverse
    {
    public static void main(String args[])
    {
    StringBuffer str=new StringBuffer(“pavan”);
    System.out.println(str.setCharAt(0,’a’));
    System.out.println(str.reverse());
    }
    }

    Sir in this program at line no:

    replacemethod.java:6: ‘void’ type not allowed here
    System.out.println(str.setCharAt(0,’a’));
    ^
    1 error this error is coming what is this meaning??

    1. When a method returns does not return a value (void), you cannot keep it in println() method. This rule applies to C/C++ also.

      str.setCharAt(0,’a’)

      setCharAt() method replaces in the original buffer. After this method call, keep only str in println() method. you get output. Following works.
      public class Demo
      {
      public static void main(String[] args)
      {
      StringBuffer str=new StringBuffer(“pavan”);
      str.setCharAt(0,’a’);
      System.out.println(str.reverse());
      }
      }

Leave a Comment

Your email address will not be published.