0

I have a task, and I need to make the code take in a user inputted word, put it in capital letters, reverse and it output it. This is my code so far, but I really have no clue how I do it.

import java.util.Scanner;

public class WordSizeChecker {

    public static void main(String[] args) {
        Scanner kb = new Scanner (System.in);
        System.out.print("Please enter a word: ");
        String oword = kb.nextLine();
        String word = oword.toUpperCase();
        int length = word.length();
        int i = 0;
        while (i < length)
        {
            System.out.println(word.substring(i,length));
            length--;
        }

    }

}

Output:

Please enter a word: chess
CHESS
CHES
CHE
CH
C

1
  • 2
    Work it out on paper, as what you would do with a word like CHESS to reverse it. And turn that in a true algorithm. Commented Feb 6, 2016 at 23:30

3 Answers 3

2
new StringBuilder(oword).reverse().toString().toUpperCase();

or with a loop

oword = oword.toUpperCase();
for(int i = oword.length() - 1; i >= 0; i--)
{
    System.out.print(oword.charAt(i));
}
System.out.println();
Sign up to request clarification or add additional context in comments.

7 Comments

while this is a very nice answer, this doesn't use an explicit while loop as the homework requested :)
Overread that, you are right :) I recommend to use the charAt() function then.
Ahaha. Works for sure but i think its missing the excercise's point
I would have failed that exercise. What a shame :D Adapted the solution for the exercise as well.
StringBuilder doesn't have a toUpperCase() method, so move it last.
|
0
public static void main(String[] args) {
    Scanner kb = new Scanner (System.in);
    System.out.print("Please enter a word: ");
    String oword = kb.nextLine();
    String word = oword.toUpperCase();
    int length = word.length();

    StringBuilder sb = new StringBuilder();
    // start from the end of the input string
    int i = length - 1;
    while (i >= 0) {
      // add the "next" character to the output
      sb.append(word.charAt(i));
      // step 1 character back
      i--;
    }
    System.out.println(sb.toString());
}

6 Comments

Try this with the String "𝖞𝖔𝖚 𝖓𝖊𝖊𝖉 𝖙𝖔 𝖈𝖔𝖓𝖘𝖎𝖉𝖊𝖗 𝖘𝖚𝖗𝖗𝖔𝖌𝖆𝖙𝖊 𝖈𝖍𝖆𝖗𝖆𝖈𝖙𝖊𝖗𝖘 𝖓𝖔𝖜𝖆𝖉𝖆𝖞𝖘"
@Ingo in that case, you'd use StringBuilder#reverse(), though I doubt this is a requirement of his task.
It is, as there are no restrictions of what the user may type.
What's the problem with unicode characters? It works.
Not with the example string from above.
|
0
String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Enter a string to reverse");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1 ; i >= 0 ; i-- )
     reverse = reverse + original.charAt(i);

  System.out.println("Reverse of entered string is: "+reverse);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.