Hey guys, I've got stucked on something here ... so to explain it here is an example.

Let's say I have a string "aa a i". My idea is to get a result that is "aA A I". -> this is a challenge on Codehunt and I got stucked on it. I think I figured out how to solve it but I can't get the coding right. So,
what it should be(I believe) first reverse the string so I have "i a aa" , then UpperCase the first char and every char that has a space before it. What I have so far (compiles but won't work correctly), where I am just trying to reverse the string, uppercase the first char, reverse back and return it with the last now letter uppercased

``````public static String Puzzle(String s) {
String ss = new StringBuilder(s).reverse().toString() ;
char[] c = ss.toCharArray();
c = Character.toUpperCase(c);
ss = new String(c);
s = new StringBuilder(ss).reverse().toString();
return s;
}
``````

## All 12 Replies

Woudld this do it?

string result = s+s.Substring(1,s.Length-1).ToUpper();

wait... I misunderstood by the example... you need to capitalize only after a space in reverse?

Yep

I can reverse and use split, then I get an array of strings in which each string in there I want to capitalize the first char

Skip reversing.

Increment through the string and append to a result string each character after you check the next to see if it is a ' ' <space> if it is ' ' convert it ToUpper else just append it, Then append the last char with ToUpper stop the for loop at -2 the length of the string so you won't go out of bounds. Try that

Um, 1 problem though .. I just made this code and it seems to work

``````public static String Puzzle(String s) {
String ss = new StringBuilder(s).reverse().toString();
//System.out.println("Reversed "+ ss);
String[] sa = ss.split(" ");
for(int i = 0; i<sa.length;i++){
//System.out.println("Split "+sa[i]);
String c = String.valueOf(sa[i].charAt(0));
String c1 = String.valueOf(Character.toUpperCase(sa[i].charAt(0)));
//System.out.println(c1);
sa[i]=sa[i].replaceFirst(c, c1);
//System.out.println(sa[i]);
sa[i] = sa[i].concat(" ");
}
StringBuilder sb = new StringBuilder();
for(int i =0; i<sa.length;i++){
sb.append(sa[i]);
}
s = sb.toString().trim();
s = new StringBuilder(s).reverse().toString();
//System.out.println(s);
return s;
}
``````

The problem here is that if my input is "a<space><space><space> " , then I get exceptions. I think it will be same if I do ur last suggestions ;(

This is C# but it does the same stuff... just convert it

string result = "";
for (int index = 0; index <s.Length - 2; index++)
if (s[index + 1] == ' ')
result += s[index].ToString().ToUpper();
else
result += s[index].ToString();
result += s.Last().ToString().ToUpper();

Insert required curly braces. The code formatting isn't working for me :(

no reversing...
put the last curly brace before the second result +=

whoops make that length -1 not length-2

Hmm, thanks for your help .. this is how I converted it and I can't seem to find any difference now but I get IndexOutOfRangeException, can you spot something wrong in this?

``````String result = "";
for (int index = 0; index <s.length() - 1; index++){
if (s.charAt(index + 1) == ' ')
result += Character.toString(s.charAt(index)).toUpperCase();
else
result += Character.toString(s.charAt(index));}
result += Character.toString(s.charAt(s.length())).toUpperCase();
return result;
``````

Last result should've been s.length()-1 , it works now thank you. Got 2 stars @ codehunt, no idea what to improve but I guess I' ll try to solve all before improving to 3 .. :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.