Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to print in capital of each frist word

 
deepak singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can any one write a small program to print capital letter of each word.
for example="this sentance i have to put in capital";
out put i want is
"This Sentance I Have To Put In Capital"
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using StringTokenizer and the charAt method of the String class. Check the API.

Bosun
 
deepak singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bosun Bello,
i know that i will get by that ,as i have writen this program
"
import java.util.*;
public class capitalizingString
{
public static String capatilize(String str)
{
StringTokenizer st=new StringTokenizer(str,"");
StringBuffer sb=new StringBuffer();
if(st.hasMoreElements())
{
String e1= (String)st.nextElement();
if(int i=0;i<st.countTokens();i++)>
{
sb.append(Character.toUpperCase(e1.charAt(i)));
sb.append(e1.substring(1)+ "");
}
}
return sb.toString();
}
public static void main(String arg[])
{
String s="this is the sentance which u have to do capitalize";
String s1=capatilize(s);
System.out.println("result of capitalizing;"+s1);
}
}
"
after running this i am geting only frist letter capital
outPut is
"This is the sentance which u have to do capitalize"
but i what out put
"This Is The Sentance Which U Have To Do Capitalize"
 
shashank bapat
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u r good at regular expressions, u can do that easily
Try gnu.regexp for java regular expressions
Thanks
Shashank
 
shashank bapat
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by deepak singh:

StringTokenizer st=new StringTokenizer(str,"");

deepak,
i am not sure if its htmlization thats causing it; but ur delim should be space and not empty string or u can use single argument constructor.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic