Professional Documents
Culture Documents
import java.util.*;
public class Main {
public static void main(String[] args) {
// fill your code here
Scanner sc =new Scanner(System.in);
System.out.println("Enter a string to reverse");
String s = sc.nextLine();
String a="";
for(int i = 0;i<(s.length());i++)
{
a = s.charAt(i) +a;
}
System.out.println("Reverse of the string is "+a);
}
Q2
import java.io.*;
import java.util.*;
import java.text.*;
public class Main{
public static void main(String args[]) throws IOException{
// fill the code
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
s = s.replaceAll("yes","s");
s = s.replaceAll("you","u");
s = s.replaceAll("today","2day");
s = s.replaceAll("why","y");
System.out.print(s);
}
Q3
import java.util.*;
class Main {
public static void main(String[] args) {
// Fill your code here
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
System.out.println("Enter the string to be searched");
String a = sc.nextLine();
System.out.println("The index of last occurence of "+(char)34+a+(char)34+" is "+s.lastIndexOf(
a));
System.out.println("Enter the index limit");
int n = sc.nextInt();
System.out.println("First occurence of "+(char)34+a+(char)34+" from "+n+"th index backward
s is "+(s.substring(0,n)).lastIndexOf(a));
}
}
Q4
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
//Your code here
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
System.out.println("Enter the start string");
String a = sc.nextLine();
if(s.startsWith(a))
{
System.out.println((char)34 +s+(char)34+" starts with "+(char)34+a+(char)34);
}
else
{
System.out.println((char)34 +s+(char)34+" does not start with "+(char)34+a+(char)34);
}
}
}
Q5
import java.util.*;
public class Main {
public static void main(String[] args) {
// fill your code here
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
System.out.println("Enter the end string");
String a = sc.nextLine();
if(s.endsWith(a))
{
System.out.println((char)34 +s+(char)34+" ends with "+(char)34+a+(char)34);
}
else
{
System.out.println((char)34 +s+(char)34+" does not end with "+(char)34+a+(char)34);
}
}
Q6
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
// fill your code here
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
String [] a = s.split(" ");
System.out.println("The words in the string are");
for(String x : a)
{
System.out.println(x);
}
}
Q7
import java.util.*;
public class Main {
public static void main(String[] args) {
// fill your code here
Scanner sc = new Scanner(System.in);
System.out.println("Enter the content of the document");
String s = sc.nextLine();
System.out.println("Enter the old name of the company");
String a = sc.nextLine();
System.out.println("Enter the new name of the company");
String b = sc.nextLine();
String c = s.replaceAll(a,b);
System.out.println("The content of the modified document is\n"+c);
}
}
Q8
import java.util.*;
class Main {
public static void main(String[] args) {
// Fill your code here
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
System.out.println("The processed string is "+s.replaceAll("\\s+"," ").trim());
}
Q9
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine();
String c = "";
int i=0;
while(true)
{
c = c + s.charAt(i);
if(i== s.length()-1)
break;
if(s.charAt(i)==s.charAt(i+1))
{
c = c +"*";
}
i++;
}
System.out.println("The processed string is "+c);
}