4 Replies - 660 Views - Last Post: 18 August 2009 - 02:05 PM Rate Topic: -----

#1 sly  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 05-December 08

reversing a word or phrase

Post icon  Posted 18 August 2009 - 01:18 PM

I am creating a program that reverses a word you just typed in, and having trouble. I am stuck and do not know what to do next...

Here is my code:

import static java.lang.System.out;
import java.util.Scanner;

public class Speech {


	public static void main(String[] args) {
		char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
				'q','r','s','t','u','v','w','u','x','y','z'};

		Scanner myScanner = new Scanner(System.in);

		out.print("Please type what you would like translated: ");


		for(int i = 0; i<(alphabet.length/2); i++) 
		{
			char temp = alphabet[i];
			alphabet[i] = alphabet[alphabet.length - i - 1];
			alphabet[alphabet.length - i - 1] = temp;
			
		}

		String inputphrase = myScanner.next();;

	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: reversing a word or phrase

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: reversing a word or phrase

Posted 18 August 2009 - 01:31 PM

what about this

:)
Was This Post Helpful? 0
  • +
  • -

#3 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: reversing a word or phrase

Posted 18 August 2009 - 01:57 PM

View Postsly, on 18 Aug, 2009 - 12:18 PM, said:

I am creating a program that reverses a word you just typed in, and having trouble. I am stuck and do not know what to do next...

Here is my code:

import static java.lang.System.out;
import java.util.Scanner;

public class Speech {


	public static void main(String[] args) {
		char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
				'q','r','s','t','u','v','w','u','x','y','z'};

		Scanner myScanner = new Scanner(System.in);

		out.print("Please type what you would like translated: ");


		for(int i = 0; i<(alphabet.length/2); i++) 
		{
			char temp = alphabet[i];
			alphabet[i] = alphabet[alphabet.length - i - 1];
			alphabet[alphabet.length - i - 1] = temp;
			
		}

		String inputphrase = myScanner.next();;

	}

}



can you plz explain what is your objectif i mean to what solution you want to arrive ?
because i red you saying something about translating and reversing but in what way ?
this code is weird
		for(int i = 0; i<(alphabet.length/2); i++) 
		{
			char temp = alphabet[i];
			alphabet[i] = alphabet[alphabet.length - i - 1];
			alphabet[alphabet.length - i - 1] = temp;
			
		}


can u plz explain what is your main purpose?
Thnx
Thnx

This post has been edited by oQMr FoxQo: 18 August 2009 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 Matamane  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 12-August 09

Re: reversing a word or phrase

Posted 18 August 2009 - 02:05 PM

Try this.

public void reverseString(String s)
{
String reverse = "";
for(int i = 0; i < s.length; i++)
{
reverse.equals(reverse + s.charAt(s.length - i - 1);
}
}


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: reversing a word or phrase

Posted 18 August 2009 - 02:05 PM

Let's try this one:

String s = "123";
for(int i = s.length-1; i > -1; i--){
   System.out.print(s.charAt(i));
}



The print statement displays each character on the same line right next to each other. Remember, the charAt(index) statement in a String is the same thing (essentially) as using the x[index] in an array. This way, you don't have to switch between String and array formats.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1