Write a java program to create strings in 2 half and display reverse of each half. Example => welcome -> wel come o/p -- lewemoc
import java.util.*;
class Stringreverse
{
String str;
//To accept string
Stringreverse()
{
Scanner s=new Scanner(System.in);
System.out.print("\nEnter string : ");
str=s.next();// next() method finds and returns the next complete token from this scanner.
}
//To create 2 half of 1 string ang reverse 2 string
void rev()
{
String s1,s2;
int i=str.length()/2;
int j;
s1=str.substring(0,i);
s2=str.substring(i,str.length());
System.out.println("1st part of string := "+s1);
System.out.println("2nd part of string := "+s2);
System.out.print("Reverse of parts of string := ");
for(j=s1.length()-1;j>=0;j--)
{
System.out.print(s1.charAt(j));
}
for(j=s2.length()-1;j>=0;j--)
{
System.out.print(s2.charAt(j));
}
}
}
//To create above class object
class TestStringreverse
{
static public void main(String args[])
{
Stringreverse obj=new Stringreverse();
obj.rev();
}
}
0 Comments