how to reverse a string in 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;
}
Also in Java:
- java scanner input int
- setbackground java
- string to arraylist convert java
- cordova cannot find symbol import com.google.firebase.iid.FirebaseMessagingService;
- java iterate through map
- compare strings in java
- java date time
- check if string is decimal java
- has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0
- stack class in java
- java find if element of list in present in another list
- java android show toast
- java string is null or empty
- how to count an replace string in java
- main method java
- java resultset to table
- how to change actionbar color in android programmatically
- java remove non numbers from string
- string isEmpty java
- Jlabel icon
- java code to concatinate integer
- java random seed
- java how to show an array
- java 8 string to localdate