OOPJ - Practical No-4
OOPJ - Practical No-4
StringBuffer class
StringBuffer class creates mutable strings that mean we can modify strings in terms of length and contents.
We can insert the character and substrings in the middle of the string, or we can append another string to
the end of string created using StringBuffer class.
String can be created using StringBuffer class using following constructors:
StringBuffer ( )
StringBuffer (String str)
Example:
StringBuffer str = new StringBuffer(“Hello”);
if(s1.equals(s2))
System.out.println("String is palindome");
else
System.out.println("String is not palindome");
}
}
2. Program to count the frequency of occurrence of a given character in a given line of text.
import java.util.*;
public class CharOccurrence
{
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==ch)
count++;
}
System.out.println("Count: "+count);
}
}
3. Program to demonstrate StringBuffer class and its methods
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
System.out.println("Original string: "+sb);
sb.append(" World");
System.out.println("After appending: "+sb);
sb.insert(5," Java");
System.out.println("After inserting: "+sb);
sb.delete(6, 12);
System.out.println("After deleting: "+sb);
sb.reverse();
System.out.println("After reversing: "+sb);
}
}