← All resources

Write a program to input a sentence and print the words in odd positions in capital letters and those in even positions in small letters.

import java.util.*;
public class Word_Alt_Case
{
    String toLowerCase(String x)
    {
        int i;
        String op="";
        char ch;
        for(i=0;i<x.length();i++)
        {
            ch = x.charAt(i);
            if(ch>='A'&&ch<='Z')
            ch = (char)(ch+32);
            op = op+ch;
        }
        return op;
    }
    String toUpperCase(String x)
    {
        int i;
        String op="";
        char ch;
        for(i=0;i<x.length();i++)
        {
            ch = x.charAt(i);
            if(ch>='a'&&ch<='z')
            ch = (char)(ch-32);
            op = op+ch;
        }
        return op;
    }
    public void change(String s)
    {
        String s2="", w="";
        int i, l, k=0;
        char ch;
        s = s+' ';
        l = s.length();
        for(i=0;i<l;i++)
        {
            ch = s.charAt(i);
            if(ch!=' ')
            {
                w = w+ch;
            }
            else
            {
                k++;
                if(k%2==0)
                w = toLowerCase(w);
                else
                w = toUpperCase(w);
                s2 = s2+w+' ';
                w="";
            }
        }
        System.out.println("Output: "+s2);
    }
    public static void main()
    {
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the string: ");
        String s = sc.nextLine();
        Word_Alt_Case obj = new Word_Alt_Case();
        obj.change(s);
    }
}