Solved

String Capitilization

Posted on 1998-04-08
4
184 Views
Last Modified: 2012-05-04
How can I capitilize the first character of the first word in a sentence?  I have random numbers working with string arrays making sentences.  I can't figure out how to capitilize the first letter of the first word?  Here's my code to print the strings:

g.drawString( article[a] + " " + noun[b] + " " + verb[c] + " " + preposition[d] + " " + article[e]
                       + " " + noun[f] + ".", 25, yPos );
0
Comment
Question by:thorner
[X]
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
4 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 10 total points
ID: 1233997
Try:

char c[1];
c[1] = (char)article[a].substring(1,1) - char("a") + char("A");
String s(c);
s += article[a].substring(2);

g.drawString( s + " " + noun[b] + " " + verb[c] + " " + preposition[d] + " " + article[e]
                           + " " + noun[f] + ".", 25, yPos );
0
 
LVL 16

Expert Comment

by:imladris
ID: 1233998
Wouldn't it be better to use the toUpperCase function in String?

g.drawString(article[a].substring(0,1).toUpperCase()+article[a].substring(1)+" "+noun[b]..... etc.


0
 

Author Comment

by:thorner
ID: 1233999
Thank you.  Both of these methods worked, however I liked the second one better because it was a shorter alternative to the first.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1234000
imladris,

Yes it would :-)  Looks like I still have some C++ vices in my coding that keep me looking  for increses execution time tricks.

In Java your solution is in fact better.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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