0

I'm relatively new to Java and I have been looking at a reverse string code for Java. I understand the majority of it, but this one part of the code I do not understand. At the For loop I am required to subtract -1 from the integer length. Why exactly am I needed to do this? Without this the code fails to execute, and I'd like to know to grasp a greater understanding of loops in the future

String start = "";

String end = "";

Scanner input = new Scanner(System.in);

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

start = input.nextLine();

int length = start.length();

for(length = length - 1 ; length >= 0 ; length--)

    end = end + start.charAt(length);

System.out.println("This string reversed is " + end);
Pendi
  • 15
  • 4
  • Because index often start with 0. Size = 3, index are 0,1,2. – Rcordoval Dec 13 '16 at 02:24
  • Where do you use `length`? In `charAt`. What restrictions does `charAt` impose on its arguments? – Sotirios Delimanolis Dec 13 '16 at 02:25
  • This is one of those problems that can be a bit more involved than you might expect. Are you going to handle only the Unicode Basic Multilingual Plane, or are you going to support Surrogate Pairs? Please see http://stackoverflow.com/questions/7476535/how-to-get-a-reversed-string-unicode-safe – Dan Armstrong Dec 13 '16 at 06:13

6 Answers6

2

Usually you can check Javadoc on Eclipse (if you are using it) to see more information about your code. Said that, this is literally from Javadoc

char java.lang.String.charAt(int index)

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If still not clear maybe an image could help:

ArrayLengthVsIndexs

Rcordoval
  • 1,932
  • 2
  • 19
  • 25
1

A string can be of any length, but when accessing .charAt() you're accessing a data structure with an index.

These work on a system that starts with the first element at 0 in Java, as opposed to 1.

For example, if you wanted to access the first letter in the string, it would be start.charAt(0).

array structure

If the length of the variable 'start' is equal to n, then the index must be n - 1 (length - 1) to access the same corresponding value/char in the data structure.

If you don't use length - 1, you effectively access a char that doesn't exist. Say that the length of 'start' is 8, then characters will only occupy .charAt(0) to .charAt(7). If you try to access .charAt(8) using just length, there is no data to access as it's beyond the array limit and therefore will fail to execute.

I'll do a quick rundown of the code too as I always feel theory helps with a familiar context. I don't wish to be patronising, I just want to reinforce the knowledge.

start = input.nextLine();

int length = start.length();

for(length = length - 1 ; length >= 0 ; length--)

    end = end + start.charAt(length);

In your code example you:

  1. Take in an input (start)

  2. Find the length of the input (length)

  3. Enter a for loop, where 'length' is subtracted by 1 to get the correct index. If 'length' is bigger than or equal to 0, it is decremented by 1 (moving the index down by 1).

  4. The char at the index position in the array is then added to the variable 'end'.

This is then repeated to take the indexed letter from the string 'start' and place them at the front of the new string 'end', therefore reversing the order.

Hope this has helped!

o.no
  • 70
  • 6
0

Strings, and more generally, arrays, are zero indexed in C, C++, Java. Character 1 is at position 0, character 2 is at position 1, etc.

For the last character, 'n', it is at position 'n-1'.

Eric
  • 601
  • 7
  • 22
0

a string of length n has characters at index 0 - (n-1), hence the initialization. It would be more appropriate to call that var index.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21
0

Most data structures are indexed starting from "0" rather than "1". (I.e. the index of the first element is "0") As such, the index of the last element is equal to the number of elements (i.e. length) - 1. Thus, if you want to start from the last element, then you must start from index length - 1.

Travis
  • 2,135
  • 17
  • 29
0

To answer your question, lets get back to some basics.

When we learnt Strings for first time, we stated its each letter's positions i.e. Index Position , starting from 0 till the end.

But here a twist comes.

When we use the length() function, it tells the total number of characters in the string, hence, it counts the first letter as 1 and and not 0 as in the case of Index Position.

So, while executing a loop which is using the string's length value, we reduce the length value by 1 so as to get the proper index positions of all the characters of the particular string.

Eg -

I am happy.

In this sentence, the length is 10 but the index position of I is 0 and similarly, the index position of y is 9.

Hope you understood the thing.