0% found this document useful (0 votes)
21 views

2nd Lab Programs

The document provides examples of Java programs that use StringBuffer and StringBuilder classes to perform operations like counting words in a string, finding character occurrences, reversing a string, concatenating strings, inserting strings at a given position, replacing substrings, deleting substrings, and demonstrating the capacity of StringBuffer.

Uploaded by

padma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2nd Lab Programs

The document provides examples of Java programs that use StringBuffer and StringBuilder classes to perform operations like counting words in a string, finding character occurrences, reversing a string, concatenating strings, inserting strings at a given position, replacing substrings, deleting substrings, and demonstrating the capacity of StringBuffer.

Uploaded by

padma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Write a Java program to count the number of words in a string using trim and split()

import java.util.*;
class CountTheWords
{
public static void main(String[] args)
{
System.out.println("Enter the string");

Scanner sc = new Scanner(System.in);

String s=sc.nextLine();

String[] words = s.trim().split(" ");

System.out.println("Number of words in the string = "+words.length);


}
}

II method
import java.util.*;
class CountTheWords
{
public static void main(String[] args)
{
System.out.println("Enter the string");

Scanner sc = new Scanner(System.in);

String s=sc.nextLine();

int count = 1;

for (int i = 0; i < s.length()-1; i++)


{
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
{
count++;
}
}

System.out.println("Number of words in a string = "+count);


}
}
2. Write a Java program to count the total number of occurrences of a given character
in a string using replace and length methods?

class CountCharacterOccurence
{
public static void main(String[] args)
{
String s = "Java is java again java again";
char c = 'a';
int count = s.length() - s.replace("a", "").length();
System.out.println(s.length());
System.out.println(s.replace("a", ""));
System.out.println(s.replace("a", "").length());
System.out.println("Number of occurances of 'a' in "+s+" = "+count);
}
}

3a. Java program to reverse a string (iterartive method)


class stringreverse {
public static void main(String[] args) {
String str = "Advanced Java";
char[] strArray = str.toCharArray();

for (int i = strArray.length - 1; i >= 0; i--)


{
System.out.print(strArray[i]);
}

}
}

3b. reverse a string using string buffer // II method


class stringreverse {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer("Advanced Java");
System.out.println(sbf.reverse());

}
}
4. write a java program prompts the user for the name of a state and displays
that states capital
import java.io.*;
class useTrim
{
public static void main(String args[])
throws IOException
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter 'stop' to quit.");
System.out.println("Enter State");
do {
str=br.readLine();
str=str.trim();
if(str.equals("Karnataka"))
System.out.println("Capital is Bengalore");
else if(str.equals("Kerala"))
System.out.println("Capital is Thiruvananthapuram");
else if(str.equals("Tamilnadu"))
System.out.println("Capital is Chennai");
}
while(!str.equals("stop"));
}
}

5. program to concate strings using StringBuffer .append method

public class StringBufferExample {


public static void main(String[] args)
{
StringBuffer sb = new StringBuffer(); //Default constructor
sb.append("Hello");
sb.append(" ");
sb.append("world");
String message = sb.toString();
System.out.println(message);
}
}

6. Program to demonstrate the different types of Constructors of StringBuffer


class
import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // now original string is changed
System.out.println(sb);
}}
7. Program to insert the given string with the string at the given position

import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java");
// Now original string is changed
System.out.println(sb);
}
}

7. b Example 2// Java Program to Illustrate StringBuffer class via insert() method

// Importing required I/O classes


import java.io.*;

// Main class
class GFG {

// Main driver method


public static void main(String[] args)
{
// Creating an object of StringBuffer class
StringBuffer s = new StringBuffer("GeeksGeeks");

// Inserting element and position as an arguments


s.insert(5, "for");
// Returns GeeksforGeeks
System.out.println(s);

s.insert(0, 5);
// Returns 5GeeksforGeeks
System.out.println(s);

s.insert(3, true);
// Returns 5GetrueeksforGeeks
System.out.println(s);

s.insert(5, 41.35d);
// Returns 5Getr41.35ueeksforGeeks
System.out.println(s);

s.insert(8, 41.35f);
// Returns 5Getr41.41.3535ueeksforGeeks
System.out.println(s);

// Declaring and initializing character array


char geeks_arr[] = { 'p', 'a', 'w', 'a', 'n' };

// Inserting character array at offset 9


s.insert(2, geeks_arr);
// Returns 5Gpawanetr41.41.3535ueeksforGeeks
System.out.println(s);
}
}

8. Program to replace the given string from the specified beginIndex and
endIndex-1.

import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}

9. StringBuffer class deletes the string from the specified beginIndex to endIndex-
1.

import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}

10. Reverse the current string using StringBuilder class.

import java.io.* ;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}

11. Write a java program to demonstrate capacity () of string Buffer class

import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // default 16
sb.append("Hello");
System.out.println(sb.capacity()); // now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());
// Now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}

You might also like