Write a program to input a string. Print the new string after converting every alternate letter to uppercase and the next immediate letter to lowercase. The special characters remain the same. INPUT : Hello India OUTPUT: HeLlO iNdIa
plz pay attention to the example
Answers
Answered by
1
Answer:
import java.util.Scanner;
public class alternative
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String sentence=scan.nextLine();
String temp="";
int flag=0;
if((int)sentence.charAt(0)>(int)('a')-1)
{
temp+=sentence.charAt(0);
flag=1;
}
else
{
temp+=sentence.charAt(0);
flag=0;
}
for(int i=1;i<sentence.length();i++)
{
if(sentence.charAt(i)==' ')
{
temp+=' ';
}
else
{
if(flag==0)
{
if(i%2!=0)
{
temp+=Character.toLowerCase((sentence.charAt(i)));
}
else
{
temp+=Character.toUpperCase((sentence.charAt(i)));
}
}
else
{
if(i%2!=0)
{
temp+=Character.toUpperCase((sentence.charAt(i)));
}
else
{
temp+=Character.toLowerCase((sentence.charAt(i)));
}
}
}
}
System.out.println(temp);
scan.close();
}
}
Similar questions