AIM:
To develop a Java program that concatenates two strings using the String class and displays the
result.
ALGORITHM :
1. Start the program
2. Declare two strings str1 = "Hello " and str2 = "World!"
3. Concatenate the two strings using + operator and store in result
4. Display the concatenated string
5. Stop the program
PROGRAM 4 :
public class SimpleConcat
{
public static void main(String[] args)
{
// Two strings
String str1 = "Hello ";
String str2 = "World!";
// Concatenate strings
String result = str1 + str2;
// Display result
[Link]("Concatenated string: " + result);
}
}
OUTPUT :
Concatenated string: Hello World!
RESULT :
Thus, the program has been executed successfully.
AIM:
To write a Java program to search a substring in a given string using the String class.
ALGORITHM :
1. Start the program
2. Declare a string str = "Hello World" and substring sub = "World"
3. Use contains() method to check if substring exists in the string
4. If found, display "Substring found", else display "Substring not found"
5. Stop the program
PROGRAM 5 :
class SubstringSearch
{
public static void main(String[] args)
{
String str = "Hello World";
String sub = "World";
if ([Link](sub))
{
[Link]("Substring found");
}
else
{
[Link]("Substring not found");
}
}
}
OUTPUT :
Substring found
RESULT :
Thus, the program has been executed successfully.
AIM:
To write a Java program to reverse a string using the StringBuffer class.
ALGORITHM :
1. Start the program
2. Create a StringBuffer object with the string value ("Hello")
3. Apply the reverse() method to the string
4. Display the reversed string
5. Stop the program
PROGRAM 8 :
class ReverseString
{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("Hello");
[Link]();
[Link](s);
}
}
OUTPUT :
OlleH
RESULT :
Thus, the program has been executed successfully.
AIM:
To write a Java program to delete a substring from a given string using the StringBuffer class.
ALGORITHM :
1. Start the program
2. Create a StringBuffer object with the string value ("HelloWorld")
3. Use delete(start, end) method to remove characters from index 5 to 10
4. Display the updated string
5. Stop the program
PROGRAM 9 :
class DeleteSubstring
{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("HelloWorld");
// delete characters from index 5 to 10
[Link](5, 10);
[Link](s);
}
}
OUTPUT :
Hello
RESULT :
Thus, the program has been executed successfully.