rtindru
rtindru

Reputation: 5357

Reverse every word in a string and capitalize the start of the word

Sample input:

abc def ghi

Sample output:

Cba Fed Ihg

This is my code:

import java.util.Stack;

public class StringRev {
    static String output1 = new String();
    static Stack<Character> stack = new Stack<Character>();
    public static void ReverseString(String input) {
        input = input + " ";
        for (int i = 0; i < input.length(); i++) {
            boolean cap = true;
            if (input.charAt(i) == ' ') {
                while (!stack.isEmpty()) {
                    if (cap) {
                        char c = stack.pop().charValue();
                        c = Character.toUpperCase(c);
                        output1 = output1 + c;
                        cap = false;
                    } else
                        output1 = output1 + stack.pop().charValue();
                }
                output1 += " ";
            } else {
                stack.push(input.charAt(i));
            }
        }
        System.out.print(output1);
    }
}

Any better solutions?

Upvotes: 4

Views: 20870

Answers (6)

Nikhil Ranjan
Nikhil Ranjan

Reputation: 1

import java.util.*;
public class CapatiliseAndReverse {
    public static void reverseCharacter(String input) {
        String result = "";
        StringBuilder revString = null;

        String split[] = input.split(" ");

        for (int i = 0; i < split.length; i++) {
            revString = new StringBuilder(split[i]);
            revString = revString.reverse();

            for (int index = 0; index < revString.length(); index++) {

                char c = revString.charAt(index);
                if (Character.isLowerCase(revString.charAt(0))) {
                    revString.setCharAt(0, Character.toUpperCase(c));
                }

                if (Character.isUpperCase(c)) {
                    revString.setCharAt(index, Character.toLowerCase(c));
                }
            }

            result = result + " " + revString;
        }

        System.out.println(result.trim());
    }

    public static void main(String[] args) {
        //System.out.println(reverseCharacter("old is GlOd"));
        Scanner sc = new Scanner(System.in);
        reverseCharacter(sc.nextLine());
    }
}

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

Make use of

StringBuilder#reverse()

Then without adding any third party libraries,

    String originalString = "abc def ghi";
    StringBuilder resultBuilder = new StringBuilder();
    for (String string : originalString.split(" ")) {
        String revStr = new StringBuilder(string).reverse().toString();
        revStr = Character.toUpperCase(revStr.charAt(0))
                + revStr.substring(1);
        resultBuilder.append(revStr).append(" ");
    }
    System.out.println(resultBuilder.toString());  //Cba Fed Ihg 

Have a Demo

Upvotes: 5

Rahul
Rahul

Reputation: 45080

You can use the StringBuffer to reverse() a string.

And then use the WordUtils#capitalize(String) method to make first letter of the string capitalized.

String str = "abc def ghi";
StringBuilder sb = new StringBuilder();
for (String s : str.split(" ")) {
    String revStr = new StringBuffer(s).reverse().toString();
    sb.append(WordUtils.capitalize(revStr)).append(" ");
}
String strReversed = sb.toString();

Upvotes: 2

Arun
Arun

Reputation: 3680

Edited

Reverse the string first and make the first character to uppercase

String string="hello jump";
StringTokenizer str = new StringTokenizer(string," ") ;
String finalString ;

while (str.hasMoreTokens()) {
   String input = str.nextToken() ;
   String reverse = new StringBuffer(input).reverse().toString();
   System.out.println(reverse);

   String output = reverse .substring(0, 1).toUpperCase() + reverse .substring(1);
   finalString=finalString+" "+output ;
}

System.out.println(finalString);

Upvotes: 1

Ashish
Ashish

Reputation: 745

1) Reverse the String with this

StringBuffer a = new StringBuffer("Java");
a.reverse();

2) To make First letter capital use StringUtils class in apache commons lang package org.apache.commons.lang.StringUtils

It makes first letter capital

capitalise(String);

Hope it helps.

Upvotes: 1

RamonBoza
RamonBoza

Reputation: 9038

public static String reverseString(final String input){
    if(null == input || isEmpty(input))
        return "";

    String result = "";
    String[] items = input.split(" ");

    for(int i = 0; i < items.length; i++){
        result += new StringBuffer(items[i]).reverse().toString();
    }

    return result.substring(0,1).toupperCase() + result.substring(1);
}

Upvotes: 1

Related Questions