Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I'm kinda new to programming so here's my problem. How can I reverse a String and then at the same time make the last letter deleted using a loop until all what's left is a single letter.

For example: 

User inputs "programming" 

The code should loop the input deleting one letter at a time (while reversed)

What should be the output is something like this:

gnimmargorp
gnimmargor
gnimmargo
gnimmarg
gnimmar
gnimma
gnimm
gnim
gni
gn
g

How can I make it done like that? Please help thanks alot!


What I have tried:

What I have tried so far is this:


public static void reverseString() {

		for (int i = phrase.length() - 1; i >= 0; i--) {
			reverse += phrase.charAt(i);
		}
		System.out.println(reverse);
	}

This will only reverse the String but not delete one letter at a time.
I'm stuck here and can't think of a possible way to do it. Please help thanks alot!
Posted
Updated 25-Mar-22 4:56am

Now that you have reversed your string, have another loop that counts down from the phrase.length. Inside that loop print a substring of the reversed phrase, starting at 1 for length of the loop index

Edit - here's a link to the substring function Java String substring() method - javatpoint[^]
 
Share this answer
 
v2
Could you please write a sample code of that for me? Thanks!
 
Share this answer
 
Comments
Dave Kreskowiak 25-Mar-22 12:15pm    
No. Doing your work for you just teaches you dependence.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900