Sentence – Convert to upper and lower Program in java

In this post, we will learn Sentence – Convert to upper and lower Program in java Programming language.

Question:

Write a program to get a sentence as input. In the first word of the sentence keep the first character as it is and change the remaining to upper case, then in the second word keep the first two characters as it is and change the remaining to upper case. Continue this pattern for the remaining words too.

Sample Input1:

java is a programming language

Sample Output1:

jAVA is a progRAMMING languAGE

Sample Input1:

good programming practice

Sample Output1:

gOOD prOGRAMMING praCTICE

CODE:

import java.util.*;
public class Main
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        String input=sc.nextLine();
        String str[]=input.split(" ");
        int n1=str.length;
        String output=new String();
        for(int i=0;i<n1;i++)
        {
            for(int j=0;j<str[i].length();j++)
            {
                if(j<=i)
                {
                    output=output+str[i].charAt(j);
                    continue;
                }
                    output=output+Character.toUpperCase(str[i].charAt(j));
            }
            output=output+" ";
        }
        System.out.println(output);
    }
}
Next:
  1. Count consecutive repeating characters
  2. Zig zag Array
Sharing Is Caring

Leave a Comment