Question

In: Computer Science

Write a method that accepts a String object as an argument and displays its contents backward....

Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method.

Sample Run
java BackwardString

Enter·a·string:Hello·world↵
dlrow·olleH↵

Solutions

Expert Solution

import java.util.*;

public class Main
{
public static void reverse(String str){
String reverse="";
int len = str.length();
for (int i = len-1 ; i >= 0 ; i--)
reverse = reverse + str.charAt(i);

System.out.println("Reverse of the string: " + reverse);
}
public static void main(String args[])
{
String str;
Scanner input = new Scanner(System.in);

System.out.println("Enter a string");
str = input.nextLine();
reverse(str);
}
}


Related Solutions

In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
Create a method named stringOperations () that accepts a string variable. This method must perform the...
Create a method named stringOperations () that accepts a string variable. This method must perform the following operations:                Split the sentence into an array                Display each element in the array on a seprate line using “ForEach”                Print the first element in the array, using the array syntax                Tell the user where the second word starts                Display the first 3 characters of the second word In your man method, ask the user to enter a sentence...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Which of this method of class String is used to obtain a length of String object?...
Which of this method of class String is used to obtain a length of String object? What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4)    break;    if(cnt==0) {     cnt++; continue; }   System.out.print(cnt + ",");   cnt++; } 1,2,3,4 Compiler error 0,1,2,3,4, 1,2,3,4,
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT