Computer Science, asked by Ashvi16, 1 year ago

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
Ashvi16: okkk....forget...actually i need all in java
QGP: Mention this in comments in all of your questions
Ashvi16: ok Thnkewh

Answers

Answered by DamEleStef
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;
}


Answered by QGP
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);
    }
}

QGP: It works for any number of words
Similar questions