‫Hello my friends, and welcome to coding Exercise number five.
‫In this exercise, we are asked to write a program to reverse a string.
‫So for example, if the user enters X, y, z, the program will print for him z, y x.
‫So pause the video and think about how to solve such a program.
‫I hope you solve it well.
‫We start by a scanner to get this string scanner or as one equal to new scanner system in.
‫And you see, I imported Java due to the scanner.
‫Now I need to display a system that output print and for the user input the string to reverse.
‫I will restore this string and transform it into a car array called letters equal to scan
‫S1 dot next line.
‫This is how we get the string and you can directly use to char array.
‫This is how we transform it into a character.
‫So test them out to print.
‫Alan, The first string we need to print.
‫So what I have done here, I get the the input string from the user using the scanner.
‫Then I stored it inside letters, character array.
‫Now I'm going to use a for loop in order to print these letters from the last to the beginning.
‫So I will start from the end.
‫Then I were passing through the middle and then I will go to the start.
‫So I am reversing the printing, not the printing.
‫The letters from from left to right.
‫I'm going to print letters from right to left.
‫So for that I will use int I equal to letters dot length.
‫I'm getting the length of this array minus one in order not to make out of bound exception.
‫And I greater than or equal to zero.
‫I'm using greater than or equal because I need the first element at index zero and the length in array.
‫As we know it's less than its index by one.
‫So here we use indices and because that I have minus one and I subtracted one and I minus minus, I'm
‫going because I'm going from end to the start.
‫I'm starting from the end and ending at the start of this system.
‫That out dot print, I'll end or print.
‫I'm not going to print them new lines, letters at index
‫then system that print.
‫New line you can print like this new line or print.
‫Let's run the program.
‫And here we go and put the string to reverse.
‫Hello.
‫If I enter it, it will give me o.
‫L.
‫E h.
‫Okay.
‫So congratulations, guys.
‫We have succeeded in getting the string from the user and reversing it through transforming it into
‫an array of characters.
‫Then we invert the loop to start from the end till reaching the start of the letters array.