Title Case

Posted on 2003-02-23
Medium Priority
Last Modified: 2012-05-04
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
Question by:Gesus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 8004483
try this..

          String c = String.valueOf(input.charAt(0)).toUpperCase();
          String output = c + input.substring(1);

Author Comment

ID: 8004647
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]==' ')
  String output = new String(stringinput);
  System.out.println (output);

Expert Comment

ID: 8005062
yes, that should also work.. did u have any problems with it?

Accepted Solution

jam40jeff earned 150 total points
ID: 8006524
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question