0% found this document useful (0 votes)
11 views3 pages

LAB3

The document discusses three Java programs: 1) a program that replaces a substring in a string, 2) a program that sorts strings alphabetically, and 3) a program that inserts a string into a specific position in a StringBuffer and displays the modified and reversed strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

LAB3

The document discusses three Java programs: 1) a program that replaces a substring in a string, 2) a program that sorts strings alphabetically, and 3) a program that inserts a string into a specific position in a StringBuffer and displays the modified and reversed strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Batch :- 4 CE B2 Enrollment no:-IU2241050254

25

LAB-3
(1)Aim :WAP to replace substring with other substring in the given string.

class Main {
public static void main(String[] args)
{
String originalString = " indus university ";
String substringToReplace = "university";
String replacement = "college";

String modifiedString = replaceSubstring(originalString, substringToReplace, replacement);

System.out.println("Original String: " + originalString);


System.out.println("Modified String: " + modifiedString);
}

public static String replaceSubstring(String original, String toReplace, String replacement) {

if (!original.contains(toReplace)) {
return original;
}
String modified = original.replaceAll(toReplace, replacement);
return modified;
}
}

OUTPUT:
Batch :- 4 CE B2 Enrollment no:-IU2241050254
25

(2)Aim:WAP that to sort given strings into alphabetical order.


import java.util.Arrays;
import java.util.Scanner;
class SortingString {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string value: ");
String str = sc.nextLine();
char charArray[] = str.toCharArray();
Arrays.sort(charArray);
System.out.println(new String(charArray));
}
}

OUTPUT:
Batch :- 4 CE B2 Enrollment no:-IU2241050254
25

(3)Aim: Create a String Buffer with some default string. Append any string to
ith position of original string and display the modified string. Also display the
reverse of modified string.

class StringBufferExample
{
public static void main(String[] args)
{
StringBuffer stringBuffer = new StringBuffer("hi program");
int i = 3;
String appendString = "Java ";
stringBuffer.insert(i, appendString);
System.out.println("Modified string: " + stringBuffer.toString());
System.out.println("Reverse of modified string: " + stringBuffer.reverse().toString());
}

OUTPUT:

You might also like