Solved

String Capitilization

Posted on 1998-04-08
4
185 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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