English

Write a program for the following. Input a string and change the text given by the user to toggle case. Example: Input: "pACIFIC is DEEP" Output: "Pacific IS deep"

Advertisements
Advertisements

Question

Write a program for the following.

Input a string and change the text given by the user to toggle case.

Example:

Input: "pACIFIC is DEEP"
Output: "Pacific IS deep"
Code Writing
Advertisements

Solution

import java.util.*;
class clTask
{
        void main()
    {
        String s1 = "", s2 = '", s3 = "";
        Scanner sc = new Scanner(System.in);
        char ch1, ch2; int count = 0;
        System.out.print(" Enter a sentence : ");
        s1 = sc.nextLine();
        for(int j = 0; j <= s1.length() -1; j++)
        {
           ch1 = s1.charAt(j);
           if(ch1 >='a' && ch1 <='z')
           {
                  s2 = s2 + (char)(ch1 – 32);
           }
           else if(ch1 >= 'A' && ch1 <='Z')
           {
                  s2 = s2 + (char)(ch1 +32);
           }
           else
           {
                  s2 = s2 + ch1;
           }
        }
        System.out.println("New String is : " + s2);
    }
}

Output:

Enter a sentence : PACIFIC is DEEP

New string is : Pacific IS deep

shaalaa.com
  Is there an error in this question or solution?
Chapter 10: String Handling - Exercises [Page 248]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 10 String Handling
Exercises | Q 3. (g) | Page 248
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×