You are on page 1of 7

EX NO: 9A

STRINGS IN JAVA
DATE:
CONCATENATE AND REVERSE THE STRING

AIM:
To write a java program to concatenate two strings and reverse the string.
ALGORITHM:
Step 1: Start the program.
Step 2: Input two strings.
Step 3: Create an object(sb) for the mutable StringBuilder class and initialize its
constructor with str.
Step 4: Concatenation-With the object sb call the append method with str1 as a
Parameter.
Step 5: Reversing: With the object sb call the reverse method .
Step 6: Print the final strings.
Step 7: Stop the program.

PROGRAM:
import java.util.Scanner;
public class javaprog
{
public static void main(String[] args)
{
String str, str1;
System.out.println("Enter the first string");
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
System.out.println("Enter the second string");
str1= sc.nextLine();
System.out.println("Concatenated String: ");
StringBuffer sb = new StringBuffer(str+str1);
System.out.println(sb);
System.out.println("Reversed String: ");
System.out.println(sb.reverse());
}
}
OUTPUT:

RESULT:
For the above program, the output was verified successfully.
EX NO: 9B
CONCATENATION OF STRINGS EXCEPT REMOVING THE
DATE: FIRST CHARACTER

AIM:
To write a java program to concatenate two strings except removing the first character
of each string.
ALGORITHM:
Step 1: Start the program.
Step 2: Input two strings.
Step 3: Create an object(sb) for the mutable StringBuilder class and initialize its
constructor with substring of str1 from the first index.
Step 4: Concatenation-With the object sb call the append method with substring of str2
from the first index as a Parameter.
Step 5: Print the final string.
Step 6: Stop the program

PROGRAM:
import java.lang.*;
public class javaprog
{
public static void main(String[] args)
{
String str1 = "PYTHON";
String str2 = "TUTORIAL";
StringBuilder s = new StringBuilder(str1.substring(1));
s.append(str2.substring(1));
System.out.println("Concatenation of the strings after removing the first character of each
string.\n "+ s);
}
OUTPUT

RESULT:
For the above program, the output was verified successfully.
EX NO: 9C
INSERT A WORD IN THE MIDDLE OF THE ANOTHER STRING
DATE:

AIM:
To write a java program to insert a word in the middle of the another string.
ALGORITHM:
Step 1: Start the program.
Step 2: Input two strings.
Step 3: Create an object(sb) for the mutable StringBuilder class and initialize its
constructor with substring of org until its middle.
Step 4: With the object sb call the append method with the word to be inserted and the
rest of substring in org.
Step 5: Print the final string.
Step 6: Stop the program.
PROGRAM
import java.lang.*;
public class javaprog
{
public static void main(String[] args)
{
String str = "Python 3.0";
String str1 = "Tutorial";
StringBuilder S= new StringBuilder(str);
S.insert(7, str);
System.out.println(S);
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.

You might also like