I want to implement a method to return a string after reversing only the alphabets [a-zA-Z]
in a sentence, not number or special characters. Reversing has to be done word-by-word. eg: for 25000rs for 1 LCD
it should be 25000sr rof 1 DCL
.
public class Main {
static String reverseWords(String inputString) {
String[] words = inputString.split(" ");
String reverseString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
reverseWord = reverseWord + word.charAt(j);
}
reverseString = reverseString + reverseWord + " ";
}
return reverseString;
}
public static void main(String[] args)
{
String str1 = "1 cup of hot coffee costs 8.00, whereas cold coffee costs 45.00.";
System.out.println(reverseWords(str1));
String str2 = "It Costs 25000rs for 1 LCD Projector.";
System.out.println(reverseWords(str2));
String str3 = "8990.33";
System.out.println(reverseWords(str3));
}
}
1 puc fo toh eeffoc stsoc 8.00, saerehw dloc eeffoc stsoc 45.00.
tI stsoC 25000sr rof 1 DCL rotcejorP.
8990.33
1 puc fo toh eeffoc stsoc,00.8 saerehw dloc eeffoc stsoc.00.54
tI stsoC sr00052 rof 1 DCL.rotcejorP
33.0998
You are just splitting at space characters, which makes every sequence of non-space characters a “word”.
This task is a canonical use case for the regex Matcher
's appendReplacement
/ appendTail
loop, which you can always use when the intended replacement logic is too complex for a simple replaceAll(String regex, String replacement)
on a String
:
static final Pattern WORD = Pattern.compile("\\pL+"); // class "L" means "letter"
static String reverseWords(String inputString) {
Matcher m = WORD.matcher(inputString);
if(!m.find()) return inputString;
StringBuffer sb = new StringBuffer(inputString.length());
do {
m.appendReplacement(sb, new StringBuilder(m.group()).reverse().toString());
} while(m.find());
return m.appendTail(sb).toString();
}
(Starting with Java 9, this API also supports using StringBuilder
instead of StringBuffer
.
But in this specific case, the operation can also be done slightly more efficient:
static String reverseWords(String inputString) {
Matcher m = WORD.matcher(inputString);
if(!m.find()) return inputString;
StringBuilder sb = new StringBuilder(inputString);
do {
for(int ix1 = m.start(), ix2 = m.end() - 1; ix1 < ix2; ix1++, ix2--) {
sb.setCharAt(ix1, inputString.charAt(ix2));
sb.setCharAt(ix2, inputString.charAt(ix1));
}
} while(m.find());
return sb.toString();
}
import java.util.*;
public class Main {
static String reverse(String inputString) {
String[] words = inputString.split(" ");
boolean set = false;
String reverseString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
if (Character.isAlphabetic(word.charAt(j))){
reverseWord = reverseWord + word.charAt(j);
set = false;
}else{
set = true ;
break ;
}
}
if(set == true){
reverseString = reverseString + reverseWord + word + " ";
}
reverseString = reverseString + reverseWord + " ";
}
return reverseString;
}
public static void main(String[] args)
{
String st1 = "1 cup of hot coffee costs 8.00, whereas cold coffee costs 45.00.";
System.out.println(Main.reverse(st1));
String st2 = "It Costs 25000rs for 1 LCD Projector.";
System.out.println(Main.reverse(st2));
String st3 = "8990.33";
System.out.println(Main.reverse(st3));
String st4 = "Hello Everyone Today we are going to learn 1-10 table.";
System.out.println(Main.reverse(st4));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.