473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Program to reverse a string

34 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class user{
  4.    public static void main(String args[])
  5.    {
  6.       String original, reverse = "";
  7.       Scanner in = new Scanner(System.in);
  8.       System.out.println("Enter a string to reverse");
  9.       original = in.nextLine();
  10.       int length = original.length();
  11.       for ( int i = length-1  ; i >= 0 ; i-- )
  12.       reverse = reverse + original.charAt(i);
  13.  
  14.       System.out.println("Reverse of entered string    is:"    +reverse);
  15.    }
  16. }
can anyone explain why the length of the string is deducted by 1...The following line:

for(int i-length-1;.....);

Thank You.
Jan 5 '15 #1
3 1488
Exequiel
288 Contributor
its deducted by 1 because when we get the length of the letters of the input we need to convert it into char array, so the array counting starts at 0, so to get the reverse output of the input we loop it reversely and to be exact to the letters of our reverse string we deducted 1.
example:
Input: LOVE
Output: EVOL
Lenght of string input is 4.
reversed counting for array inside the for loop starts at 3 and end to 0.
so
array[3] = E.
array[2] = V.
array[1] = O.
array[0] = L.


this is the sample code i made.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class palindrome
  4. {
  5.     public static void main(String [] args)
  6.     {
  7.     BufferedReader dataIn= new BufferedReader (new InputStreamReader(System.in));
  8.  
  9.         String reversed="";
  10.         String input="";
  11.         int x=0;
  12.         String sd="";
  13.  
  14.         try{
  15.  
  16.             System.out.println("Input word to reverse and Determine If it is a palindrome or not! ");
  17.             input=dataIn.readLine();
  18.              char toreverse[]=input.toCharArray();    
  19.  
  20.             for(x=toreverse.length-(1); x>=0; x--)
  21.             {
  22.                 reversed+=toreverse[x];    
  23.             }
  24.             System.out.println("Reverse of entered string is: "+reversed);
  25.  
  26.         }catch (IOException e){
  27.             System.out.println("error!");}
  28.  
  29.     }
  30. }
Jan 5 '15 #2
abhishekphukan
34 New Member
Thnx a lot for clearifying it...:)
i was just stuck at that line...thank you.
Jan 5 '15 #3
Exequiel
288 Contributor
Your welcome. I'm glad to help. goodluck :)
Jan 5 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry | last post by:
Hi, I want to draw a reverse string, does anyone know how to implement it? And I want to draw a vertical string, it can be implemented by setting StringFormat.StringFormatFlags...
9
by: HELLO $$$ | last post by:
From Beginner : I am studying a C++ program, using: char, arrays, etc. The aim of this program is to let it make "Reverse" of any statement (string) as example: if the original string is " this...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
2
by: yesh81 | last post by:
Hi , can anybody write a java program to validate IP Addresses using string tokenizer.
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
3
by: acs7737 | last post by:
i have this hw problem for class thats supposed to read a comma separated list of my cd collection ex.: artist,album artist,album artist,album it reads that into a string and then has to separate...
1
by: DHARMISTHA | last post by:
how to write java pro of to rewite STRING TO GINRST IN alphabetical order ?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.