
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse a Given String While Preserving Space Position in Java
You can reverse the contents of a given String using leaving the spaces using the reverse() method of the StringBuffer class.
Example
public class Test { public static void main(String args[]) { String str = "hi welcome to Tutorialspoint"; String strArray[] = str.split(" "); StringBuffer sb= new StringBuffer(str); sb.reverse(); for(int i=0 ; i<str.length(); i++){ if(str.charAt(i)== ' '){ sb.insert(i, " "); } } sb.append(""); System.out.println(sb); }
Output
tn iopslai ro tuT ot emoclew ih
Advertisements