Advertisements
Advertisements
Question
Write a program for the following.
Input a string (in lower case) and capitalise the 2nd letter of each word of the sentence.
Code Writing
Advertisements
Solution
import java.util.*;
class clTask
{
void main()
{
String s1 = "", s2 = "";
char ch1, ch2;
Scanner sc = new Scanner(System.in);
System.out.print(" Enter a sentence in lower case : ");
s1 = sc.nextLine();
s1 = " " + s1; // putting a leading space
for(int j = 0; j<= s1.length() – 1; j++)
{
ch1 = s1.charAt(j);
if(ch1='')
{
ch2 = s1.charAt(j + 2);
ch2 = Character.toUpperCase(ch2);
s2 = s2 + ch1 + s1.charAt(j + 1) + ch2;
j =j + 2;
}
else
s2 = s2 + ch1;
}
System.out.println("Modified String is : " + s2);
}
}
Output:
Enter a sentence in lower case : failures are the pillars of success Modified String is: fAilures aRe tHe pIllars oF sUccess
shaalaa.com
Is there an error in this question or solution?
