4 Replies - 653 Views - Last Post: 26 January 2010 - 05:43 AM Rate Topic: -----

#1 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Reverse a string

Post icon  Posted 24 January 2010 - 11:28 PM

import java.io.*;
class str 
{
public static void main(String[] args) 
{
String temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
try 
{
temp = br.readLine();
int n = temp.length();
char name[] = temp.toCharArray();
for (int i = 0; i < n / 2; i++) 
{
char temp1 = 0;
name[i] = temp1;
name[n-1] = name[i];
temp1 = name[n-1];
}
System.out.println(name);
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
}



i am not getting the desired output for example if i enter a string called Hello then iam getting only ll as output..

Plz help!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Reverse a string

#2 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Reverse a string

Posted 25 January 2010 - 12:36 AM

Do you have to use BufferedReader and InputStreamReader?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reverse a string

Posted 25 January 2010 - 01:03 AM

...
for (int i = 0; i < n / 2; i++)
{
char temp1 = 0;
name[i] = temp1;
name[n-1] = name[i];
temp1 = name[n-1];
}
...


You have several problems here.
  • You are only scanning (possibly) half the number of characters in the array - n/2
  • for each iteration of the for() loop you set temp1 to zero
  • name[i] will always be zero as a result
  • name[n-1] will always be the last character
  • setting temp1 = name[n-1] doesn't do anything as temp1 will be zeroed the next time through the loop

Since you are using a character array, why not use a second array to hold the reversed string? Here's what I did with your problem:
public static void main(String[] args) {
	// init all the variables we will use.
	int index = 0;
	int n = 0;
	String temp = "";

	try {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Enter the string");

		temp = br.readLine();

		br.close();
	} catch (IOException e) {
		e.printStackTrace();
		// all errors are fatal.  :)
		return;
	}

	// get the user's string length
	n = temp.length();
	// set the index to the last character in the array
	index = n - 1;

	// create our input array
	char[] name = temp.toCharArray();

	// now create our output array
	char[] reversed = new char[n];

	// copy the input array to the output array in reversed order
	for (int i = 0; i < n; i++)
		reversed[index--] = name[i];

	System.out.println(reversed);
}


Here's the output:

Quote

Enter the string
this is a test
tset a si siht

Was This Post Helpful? 1
  • +
  • -

#4 vyom.dev  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 74
  • Joined: 13-January 10

Re: Reverse a string

Posted 25 January 2010 - 01:15 AM

View Postn8wxs, on 25 Jan, 2010 - 12:03 AM, said:

here's what i did to your code


Thank You very much......actually i have just started to learn java.....but i wanna ask you when would i be so proefficient in java as you are that i will check the problems in my code myself.... is this happens to all the novice programmers or i cant learn this language.... :(
Plus i want to know was my logic correct????


thanx u rock buddy :^:

This post has been edited by vyom.dev: 25 January 2010 - 01:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reverse a string

Posted 26 January 2010 - 05:43 AM

Proficiency will come with practice and learning. :)

You had all the basic pieces in your code. You were doing ok up to thefor()
loop. :)

I have reworked the code I posted earlier to use the shift-in-place technique
your were trying, I think :), to code.

Have a look at:
public static void main(String[] args) {
	// init all the variables we will use.
	int index = 0;
	int n = 0;
	String temp = "";

	try {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("Enter the string");

		temp = br.readLine();

		br.close();
	} catch (IOException e) {
		e.printStackTrace();
		// all errors are fatal.  :)
		return;
	}

	// create our input array
	char name[] = temp.toCharArray();

	// get the user's string length
	n = temp.length();
	// set the index to the last character in the array
	index = n - 1;

	// character to hold the character we are moving
	char c;

	for (int i = 0; i < n / 2; i++) {
		// save character
		c = name[i];
		// copy character from other end of string
		name[i] = name[index];
		// and move the saved character to that end  :)
		name[index--] = c;
	}

	System.out.println(name);
}


This post has been edited by n8wxs: 26 January 2010 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1