Title Case

i want to read in a string and output it in title case (so every capital after a space (of a few spaces) will be capitalised.

obviously to do this i'll have to change the string into an array of chars, but i'm not sure how to capitalise then, since:

   char stringinput[] = input.toCharArray();
   stringinput[0] = stringinput[0].toUpperCase();

doesn't seem to work.

that code there is just to capitalise the 1st letter, but i'm not sure how to do this
GesusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vemulCommented:
try this..

          String c = String.valueOf(input.charAt(0)).toUpperCase();
          String output = c + input.substring(1);
0
GesusAuthor Commented:
i used


// prints input in title case
 void title(String input)
 {
  int len;
  int i;
  input = input.toLowerCase();
  len = input.length();
  char stringinput[] = input.toCharArray();
  stringinput[0] = Character.toUpperCase(stringinput[0]);
  for (i=0; i+1!= len ;i++)
  {
   if (stringinput[i]==' ')
   {
    stringinput[i+1]=Character.toUpperCase(stringinput[i+1]);
   }
  }
  String output = new String(stringinput);
  System.out.println (output);
 }
0
vemulCommented:
yes, that should also work.. did u have any problems with it?
0
jam40jeffCommented:
Your code looks like it should work unless the string ends with a space character.  Your loop should begin with

for(i = 0; i < len - 1; i++)

so that it does not test the last character.  If it tests the last character (i = len - 1) and this character is equal to ' ', then stringinput[i + 1] will throw a NullPointerException.

Otherwise, the code looks like it should work perfectly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.