( Capitalize first letter of each word ) Write the following method that returns
ID: 3569112 • Letter: #
Question
(Capitalize first letter of each word) Write the following method that returns a new string in which the first letter in each word is capitalized.
public static void title(String s)
Write a test program that prompts the user to enter a string and invokes this method, and displays the return value from this method. Here is a sample run:
<output>
Enter a string: london england 2015 <enter icon>
The new string is: London England 2015
<end output>
Note that words may be separated by multiple blank spaces.
Explanation / Answer
import java.util.Scanner;
public class Lab11
{
public static void conv(String s){
String c = ""+s.charAt(0);
String m=s;
s="";
c=c.toUpperCase();
for(int i=0;i<m.length();i++){
if(i==0){
s=s+c;
}
else{
s=s+m.charAt(i);
}
}
System.out.println("The new String is: "+s);
//s.CharAt(0, Character.toUpperCase(c));
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("How many words do you want to enter:");
int n=s.nextInt();
for(int i=0;i<n;i++){
System.out.println("enter a String:");
String s1=s.next();
conv(s1);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.