how to create a java program that ask the user to input a string and to input a number of how many times you want to reverse it at right to left?
please help me with creating this program..
sample:
Enter word:
computer
enter number of rotation:
3
output:
tercompu

Recommended Answers

All 13 Replies

Okay. If you are able to get the user to enter a string, reversing it is easy. Just say StringBuffer(input).reverse().toString(), where "input" is the string which the user has entered, and you now have the reversed version of the string which you can then feed back to the user.

Reverse or Rotate? The requirement and title are contradictory.

i just want the whole program of it please!, im new to java

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

I WILL GIVE YOU A CPP CODE FOR THE SAME I DONT KNOW ABOUT JAVA YOU CAN HAVE THE LOGIC

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>


void main()
{
char A[100],B[100];
int n,c,i,r;
cout<<"\n Enter the string : ";
gets(A);
i=0;
n=0;
while(A[i]!='\0')
{
i++;
n++;
}

strcpy(B,A);
cout<<"\n Enter the rotation factor : ";
cin>>r;
for(i=0;i<n;i++)
{
c=(i+r)%n;
A[c]=B[i];
}
cout<<"\n The updated string is : "<<A;
getch();
}

of course!!

        System.out.println("Enter word: ");
        String name=datain.readLine();
        for(int i=name.length()-1; i>=0; i--)
            System.out.print(name.charAt(i));
        System.out.println();

the code above is my code.. it reverses the whole string completely.. what i cannot do is to ask the user to input how many times he want to reverse per letter..

commented: it is reversion!!!!!!!!!!!!!!!! +0

please help me.. if it's okay

You keep saying "reverse" but the sample you gave was

Enter word:
computer
enter number of rotation:
3
output:
tercompu

... which is rotation, not reversing. Also "how many times to reverse|" makes no sense - if you reverse it twice you get back to the original string (etc).

In your sample you rotate an 8 letter word by 3 chars - which means you take the last three characters and move them to the front. In other words:
rotated string = (last 3 letters of original string) + (first 5 letters of original string).
Now look at the substring method of the String class - it allows you the extract parts of a string just like that.

it is not reversion it is rotation and thats what i have given

nevermind

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.