Solved

String Capitilization

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
create a gui in perl 3 46
Requested array size exceeds VM limit 3 51
backtracking recursion  code 19 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now