Question

In: Computer Science

I need to reverse strings with spaces using the nextLine() with Scanner in the following code:...

I need to reverse strings with spaces using the nextLine() with Scanner in the following code:

package Chapter8;

//To import the necessary libraries

import java.util.Scanner;

public class BackwardString

{

public static void main(String[] args)

{

//To read string from user input

String input;

Scanner scanner = new Scanner(System.in);

System.out.print("Enter String here : ");

input=scanner.next();

//To reverse passed string

backward(input);

//To close Scanner object

scanner.close();

}

//To reverse the input string

private static void backward(String source)

{

int i, len = source.length();

StringBuilder dest = new StringBuilder(len);

for (i = (len - 1); i >= 0; i--)

{

dest.append(source.charAt(i));

}

System.out.println("Reversed String : "+dest);

}

}

Thank you!

Solutions

Expert Solution

If you have any doubts, please give me comment...

package Chapter8;

//To import the necessary libraries

import java.util.Scanner;

public class BackwardString {

    public static void main(String[] args) {

        // To read string from user input

        String input;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter String here : ");

        input = scanner.nextLine();

        // To reverse passed string

        backward(input);

        // To close Scanner object

        scanner.close();

    }

    // To reverse the input string

    private static void backward(String source) {

        int i, len = source.length();

        StringBuilder dest = new StringBuilder(len);

        for (i = (len - 1); i >= 0; i--) {

            dest.append(source.charAt(i));

        }

        System.out.println("Reversed String : " + dest);

    }

}


Related Solutions

Code a program which asks user to enter two strings using Scanner class and returns the...
Code a program which asks user to enter two strings using Scanner class and returns the total number of non-space characters in the Strings. For ex: “Hi there” and “hello” should return 12.
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item on 1st column, price on 2nd) Stewing beef,15.45 Ground beef,11.29 Pork chops,11.72 Chicken,7.29 Bacon,7.12 Wieners,4.33 Canned salmon,5.68 Homogenized milk,5.79 Partly skimmed milk,5.20 Butter,4.99 Processed cheese slices,2.53 Evaporated milk,1.89 Eggs,3.11 Bread,2.74 Soda crackers,3.27 Macaroni,1.45 Flour,4.54 Corn flakes,5.72 Apples,4.71 Bananas,1.56 Oranges,3.70 ... a. In the function createPricesDict(), create a dictionary of each product mapped to its price. b. Suppose we have another dictionary for our cart...
For the following code I need to open the file of a persons choice which I...
For the following code I need to open the file of a persons choice which I did. I can get it to read the file, but I need help with counting lines and characters feel like i have the code I just cant make it work. Either way, the program should then ask the user Analyze another file (y/n)? and repeat the process again (asking for a file and analyzing its meta-data). This should continue repeating until the user chooses...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
DTMF home automation system using ATMEGA8 i need the circuit and the code for the microcontroller
DTMF home automation system using ATMEGA8 i need the circuit and the code for the microcontroller
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT