reverse a string in java
Java
public class ReverseString {
public static void main(String[] args) {
String s1 = "neelendra";
for(int i=s1.length()-1;i>=0;i--)
{
System.out.print(s1.charAt(i));
}
}
}//make a scanner object
Scanner input = new Scanner(System.in);
//take in an input string and store it inside a <String> variable
System.out.println("Enter the string: ");
String string = input.nextLine();
//convert string to character arraylist by first creating
//an array list for the characters in correct order
//and an array list for what we will use
//to store the characters in reverse order later in the code
ArrayList<Character> chars = new ArrayList<Character>();
ArrayList<Character> reversed = new ArrayList<Character>();
//put all the characters from string into chars arrayList
for (char c : string.toCharArray()) {
chars.add(c);
}
//get the size of the arraylist
Integer length = chars.size() - 1;
Integer x = 0;
//loop all the elements in the array chars into reversed arraylist in reverse order
while(x < chars.size()) {
reversed.add(chars.get(length));
length -= 1;
x += 1;
}String str = "Hello";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}// Java program to ReverseString using StringBuilder
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString
{
public static void main(String[] args)
{
String input = "Geeks for Geeks";
StringBuilder input1 = new StringBuilder();
// append a string into StringBuilder input1
input1.append(input);
// reverse StringBuilder input1
input1 = input1.reverse();
// print reversed String
System.out.println(input1);
}
}
Also in Java:
- Title
- how to create a Scanner Object in java
- Category
- Java
- Title
- how to play a clip of audio in java when needed
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- sc.nextline skips
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- java stream sort Collator
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- java convert a char[] to string
- Category
- Java
- Title
- java vs javascript
- Category
- Java
- Title
- how to make it another player's turn java
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- java processbuilder example
- Category
- Java
- Title
- java remove first character from string
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- create notification android
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- javafx action event enter key
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- java bigdecimal compareto
- Category
- Java
- Title
- java map example
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- iterate through an arraylist java
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- jtable font size
- Category
- Java
- Title
- loop array using stream java
- Category
- Java
- Title
- from file to array java
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- convert java list to array
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- run static method java
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- java string array
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- how to know if String is the same java
- Category
- Java
- Title
- and roid shape setCornerRadii
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- Enums injava
- Category
- Java
- Title
- java read from connection even if 404
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- Compare integers java sort
- Category
- Java
- Title
- how to use for loop for array in java
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- how to set frame colo in java
- Category
- Java
- Title
- java int to int array
- Category
- Java
- Title
- switch case java
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- foreach java
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- java 8 stream group by example
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- java arraylist to string
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- how to take binary input in java
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- how to add java_home in mac
- Category
- Java
- Title
- store string elements in character array in java
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- intellij set target bytecode version
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- how to remove duplicates from an array java
- Category
- Java
- Title
- connect 2 package in android
- Category
- Java
- Title
- java get last element of list
- Category
- Java
- Title
- reverse linked list in java to get both head and tail
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- color from hex code flutter
- Category
- Java
- Title
- java treeset
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- append to arraylist by index java
- Category
- Java
- Title
- java string reverse
- Category
- Java
- Title
- java get annotation value
- Category
- Java
- Title
- fibonacci sequence java using recursion
- Category
- Java
- Title
- threadsafe singleton pattern in java
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- arraylist with values java
- Category
- Java
- Title
- java parse xml string
- Category
- Java
- Title
- java lambda list of objects cast
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- do statement java
- Category
- Java