Palindrome Delete Replace Reverse Java Example

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.

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.

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. M.pavankumarreddy

    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. S. Nageswara Rao, Corporate Trainer

      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 Reply

Your email address will not be published. Required fields are marked *