Write a program to manipulate strings in java.
Program Algorithm:
Step 1: Create a class with the main function.
Step2: Get a string input from the user using the BufferedrEADER STREAM.
Step 3:call the following functions on the string: .toLowerCase(),.toUpperCase()
Step 4:Place an integer in the wrapper class Integer and parse it to strings using toString()
Step 5:Try calling 4 more methods on the strings.
Program Code:
import java.io.*;
class StrMan
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String:");
String s=b.readLine();
System.out.println("Trimmed String:"+s.trim());
System.out.println("Entered string in lowwer case: "+s.toLowerCase());
System.out.println("Entered string in upper case: "+s.toUpperCase());
Integer a=new Integer(8);
System.out.println("a= "+ a);
System.out.print("a.toString():");
System.out.println(a.toString());
System.out.println("Enter the position to extract character:");
int p=Integer.parseInt(b.readLine());
if(p<=s.length())
System.out.println("Charachter at pos "+p+" in String "+s+" is '"+s.charAt(p-1)+"'.");
else
System.out.println("Valid position not entered.");
for(int i=0;i<s.length();i++)
System.out.println("Index of '"+s.charAt(i)+"' is "+s.indexOf(s.charAt(i)));
System.out.println("Enter the index to replace charachter:");
p=Integer.parseInt(b.readLine());
if(p<s.length())
{
System.out.println("Enter the charachter to replace with:");
char c=(char)b.read();
System.out.println("Replaced String:"+s.replace(s.charAt(p),c));
}
else
System.out.println("Enter a valid position.");
}
}
Sample Inputs and Outputs:
Enter a String:
Ankit
Trimmed String:Ankit
Entered string in lowwer case: ankit
Entered string in upper case: ANKIT
a= 8
a.toString():8
Enter the position to extract character:
2
Charachter at pos 2 in String Ankit is 'A'.
Index of is 0
Index of 'A' is 1
Index of 'n' is 2
Index of 'k' is 3
Index of 'i' is 4
Index of 't' is 5
Enter the index to replace charachter:
0
Enter the charachter to replace with:
S
Replaced String:SAnkit
Post A Comment:
0 comments: