?
Solved

String Capitilization

Posted on 1998-04-08
4
Medium Priority
?
188 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 40 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
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…
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 different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

770 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