Showing posts with label Reverse the Words of String. Show all posts
Showing posts with label Reverse the Words of String. Show all posts

Reverse the Words of String using JAVA

Reverse the Words of String using JAVA

class A1
{
public static void main(String args[])
{

//Input String
String text="gardi mca gtu sooadm java";

//Output String
String s1="";

int k2,k1,i=0,j=0;
//Set both pointer to End of String
k2=k1=text.length()-1;

//Loop for moving in String from END to START
for(i=k2;i>=0;i--,k1--)
{

//Checking for SPACE and First Character
 if(text.charAt(i)==' ' || i==0)
{

//Creating New String
for(j=(i==0)?0:i+1;j<=k2;j++)
{
s1=s1+text.charAt(j);
} // For Loop of j

s1=s1+' ';
k2=k1-1;

} // IF condition

} //For Loop of i

System.out.println(s1);
} // main

} //Class