The document describes an experiment with 4 aims: 1) check String and StringBuffer length and capacity, 2) reverse a input string and convert to uppercase, 3) append a input string, and 4) extract a substring. It compares Strings, which are immutable, to StringBuffers, which are mutable. Key methods for StringBuffer include length(), capacity(), and append(). The program demonstrates these aims by getting user input, reversing and uppercase converting and appending strings, and extracting substrings.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
48 views
Experiment No 3
The document describes an experiment with 4 aims: 1) check String and StringBuffer length and capacity, 2) reverse a input string and convert to uppercase, 3) append a input string, and 4) extract a substring. It compares Strings, which are immutable, to StringBuffers, which are mutable. Key methods for StringBuffer include length(), capacity(), and append(). The program demonstrates these aims by getting user input, reversing and uppercase converting and appending strings, and extracting substrings.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Experiment No: 3
Aim: To perform following operations
i) Check the length and capacity of String and StringBuffer objects ii) Reverse the contents of a string given on console and convert the resultant string in Upper Case. iii) Input a string from the console and append it to above resultant string. iv) Extract the substring from resultant string. Theory: Java provides String and String Buffer classes. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences. String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified. StringBuffer Class Construcors are 1.StringBuffer() : Creates an empty string buffer whose initial capacity is 16 characters. 2. StringBuffer(int): Creates an empty string buffer with the specified initial capacity. 3. StringBuffer(String): Creates a string buffer whose value is initialized by the specified String. The capacity of the string buffer is the length of the original string plus 16. Some of the most used methods are 1. length() and capacity():The length of a StringBuffer can be found by the length( ) method, while the total allocated capacity can be found by the capacity( ) method. 2. append():It is used to add text at the end of the existence text. Some of the Constructors used in String Class are 1. String(): Create an empty string 2.String(byte[]) : Creates a string whose value is set from the contents of an array of bytes. 3.String(char[]) : Creates a string whose value is set from the contents of an array of characters. 4.String(String) : Creates a string whose value is set from another string. Using this constructor with a literal string argument is not recommended, because it creates two identical strings.
5.String(StringBuffer): Creates a string whose value is set from a string buffer.
6.String(StringBuilder) : Creates a string whose value is set from a string builder.
Program: import java.util.*; class Cases { //length and capacity of string and stringbuffer String s="Jain University"; //String initialization StringBuffer sb1=new StringBuffer(); //String buffer with default size but no input StringBuffer sb2=new StringBuffer("SETJU"); //String buffer with input StringBuffer sb3=new StringBuffer(50); //String buffer with capacity input void demo1() { System.out.println("Case1"); System.out.println("with input, string length is: "+s.length()); //String length System.out.println("Before input, stringbuffer length is: "+sb1.length()); //StringBuffer length System.out.println("With input, stringbuffer length is: "+sb2.length()); System.out.println("With capacity input, stringbuffer length is: "+sb3.length()); System.out.println("Before input, stringbuffer length is: "+sb1.capacity()); /* StringBuffer capacity */ System.out.println("With input, stringbuffer length is: "+sb2.capacity()); System.out.println("With capacity input, stringbuffer length is: "+sb3.capacity()); }
String demo2(String read)
{ String reverse = ""; int i=read.length()-1; while(i>=0) { reverse = reverse + read.charAt(i--); } return reverse; } String demo3(String read1, String read2) { return read1.concat(read2); } void demo4(String read) { System.out.println("\nCase4"); System.out.println("Given only begining index as 2 "+read.substring(2)); System.out.println("Given begining and end index as (2 to 4) "+read.substring(2,4)); } } public class Three { public static void main(String args[]) { Scanner sc=new Scanner(System.in); Cases c1=new Cases(); c1.demo1(); System.out.println("\nCase2"); System.out.println("Enter string to reverse: "); String s1=sc.nextLine(); String res1=c1.demo2(s1); System.out.println("Reversed string is "+ res1.toUpperCase()); System.out.println("\nCase3"); System.out.println("Enter string to append: "); String s2=sc.nextLine(); String res2=c1.demo3(res1,s2); System.out.println("Concatenated string is "+res2); c1.demo4(res2); } }