- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to reverse a given String with preserving the position of space.
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
- Related Articles
- Program to reverse the position of each word of a given string in Python
- Java program to reverse an array upto a given position
- Java Program to Reverse a String
- How to reverse a given string in Java?
- Program to reverse an array up to a given position in Python
- Java program to print the reverse of the given number
- Java program to reverse a string using recursion
- Java Program to Reverse a String Using Stacks
- Reverse words in a given String in Java
- Write program to reverse a String without using reverse() method in Java?
- Write a java program to reverse each word in string?
- C# program to reverse a string
- Java program to extract ‘k’ bits from a given position
- Java program to reverse an array in groups of given size
- Write a java program reverse tOGGLE each word in the string?

Advertisements