Ask Question

Write a "for loop" that displays a string in the reverse order (DO NOT use the reverse method) 3.

+2
Answers (1)
  1. 21 April, 15:57
    0
    Following are the code in java to reverse any string without using reverse function.

    import java. util.*; / / import package

    class Main / / class

    {

    public static void main (String args[]) / / main class in java

    {

    String s, r = ""; / / string variable

    Scanner out = new Scanner (System. in); / / scanner classes to take input

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

    s = out. nextLine (); / / input string

    int l1 = s. length (); / / finding length of string

    l1=l1-1;

    for (int i = l1; i > = 0; i--) / / for loop to reverse any string

    {

    r = r + s. charAt (i);

    }

    System. out. println (" The Reverse String is: "+r); / / print reverse string

    }

    }

    Explanation:

    firstly we input any string, finding the length of that string then after that iterating over the loop by using for loop and last display that reverse string

    output

    Enter a string to reverse

    san ran

    The Reverse String is: nar nas
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a "for loop" that displays a string in the reverse order (DO NOT use the reverse method) 3. ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers