With Loop
With Loop you can easily do it by converting String into Charcter using charAt() inside a for Loop or any other Loop.
You have to give the condition to terminate, when it reaches Strings Length with the help of length() static method of String class. In this program there is no need to call a User-defined Method.
Each time charAt is getting different values because of the for Loop which extracts every Character from the given String. If you want print the Characters in the same line you can use print instead of println. To get an Integer you have to use nextInt() in the Scanner Class as well as for String you have to use next() to read.
Program
Output
Without Loop
Here you have to call a user-defined method and you have to print those Characters with the same pre-defined method called charAt(). The only difference between these two program is here you have to use a function called Recursion which calls itself.
While calling a method you can pass as many argument as you want for this you have to pass two arguments a String got from user and a Number assigned as zero.
As you know that once the return keyword is used it will return to the methods caller. From this you have to use the return keyword when the number is equalto Strings Length or else the Recursion function calls itself again and again and you have to place a increment value as 1 for the number. If you forget to do this increment it will go infinte. Between this you have to print those characters using charAt().
Program
Output
Here is the link for Google KickStart Round A 2020 Allocation problem using JAVA.
0 Comments