WAP to accept a string and display the new string after reversing the letters/characters of the word.
for example -input: happy summer vacation
output: yppah remmus noitacaV
(in java)
QGP:
Don't forget to mention everywhere that you want program in JAVA. Else, mention language
Answers
Answered by
4
Dude just reverse the loop .....
l=s.length();
for(i=l-1;i>=0;i--)
{
ch=s.charAt(i);
if(ch!=' ')
{
s1=s1+ch;
}
else
s1=s1+ch;
}
l=s.length();
for(i=l-1;i>=0;i--)
{
ch=s.charAt(i);
if(ch!=' ')
{
s1=s1+ch;
}
else
s1=s1+ch;
}
Answered by
3
import java.util.*;
public class Reverser
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String st1="",sr="",st2="",st3="";
int i,x,y,z;
char b=0;
System.out.print("Enter a string: ");
st1 = in.nextLine();
st1=st1+" ";
x = st1.length();
for (y=0;y<x;y++)
{
b = st1.charAt(y);
if (b==' ')
{
st2=st2+" "+st3;
st3="";
}
else
{
st3=b+st3;
}
}
System.out.println("String in reverse order is: "+st2);
}
}
public class Reverser
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String st1="",sr="",st2="",st3="";
int i,x,y,z;
char b=0;
System.out.print("Enter a string: ");
st1 = in.nextLine();
st1=st1+" ";
x = st1.length();
for (y=0;y<x;y++)
{
b = st1.charAt(y);
if (b==' ')
{
st2=st2+" "+st3;
st3="";
}
else
{
st3=b+st3;
}
}
System.out.println("String in reverse order is: "+st2);
}
}
Similar questions