scbushan05
7/6/2017 - 10:58 AM

StringReverseTwo.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaprograms;

/**
 *
 * @author bhushan_chandranna
 */
public class StringReverseTwo {
    public static void main(String[] args) {
        System.out.println(reverseString("(Hello) ^this& *is, ,bushan!"));
    }
    public static String reverseString(String string){
        String temp = "";
        boolean flag = false;
        for(int i = string.length() - 1; i >= 0; i--){
            if(flag == false){
                int c = string.charAt(i);
                if((c >= 33 && c <= 64) || (c >= 91 && c <= 96)){
                    temp = temp + string.charAt(i);
                    flag = false;
                }else{
                    temp += String.valueOf(string.charAt(i)).toUpperCase();
                    flag = true;
                }
            }
            else if(String.valueOf(string.charAt(i)).equals(" ")){
                int c = string.charAt(i-1);
                if((c >= 33 && c <= 64) || (c >= 91 && c <= 96)){
                    temp = temp +" "+ string.charAt(i - 1);
                    i--;
                    flag = false;
                }else{
                    temp += " "+String.valueOf(string.charAt(i - 1)).toUpperCase();
                    i--;
                }
            }
            else{
                temp += String.valueOf(string.charAt(i)).toLowerCase();
            }
        }
        return temp;
    }
}