Write a Program to Toggle each character in a string

27 June 2022

Write a Program to Toggle each character in a string


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get an input string from user and then convert the lower case of alphabets to upper case and all upper-case alphabets into lower case.

 

Input

Hello

Output

hELLO

 

C Program

#include<stdio.h>

#include<string.h>

int main()

{

              char str[50];

              int count;

              printf("Enter a String: ");

              scanf("%s",str);

              for (count = 0; str[count]!='\0'; count++)

              {

                  if(str[count] >= 'A' && str[count] <= 'Z')

            {

              str[count] = str[count] + 32;

            }

            else if(str[count] >= 'a' && str[count] <= 'z')

            {

                    str[count] = str[count] - 32;

                  }                      

              }

              printf("Toggoled string is: %s", str);

              return 0;

}

 

 

C++ Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

              char str[50];

              int count;

              cout<<"Enter a string: ";

              cin>>str;

              for (count = 0; str[count]!='\0'; count++)

              {

                  if(str[count] >= 'A' && str[count] <= 'Z')

            {

              str[count] = str[count] + 32;

            }

            else if(str[count] >= 'a' && str[count] <= 'z')

            {

                    str[count] = str[count] - 32;

                  }                      

              }

              cout<<"Toggoled string is: "<<str;

              return 0;

}

 

Java

import java.util.Scanner;

public class Main {

              public static void main(String[] args) {

               Scanner sc = new Scanner(System.in);

               System.out.print("Enter a String : ");

               String str = sc.nextLine();

               String s1 = "";

               for (int i = 0; i < str.length(); i++) {

                             if(Character.isUpperCase(str.charAt(i)))

                                            s1=s1+Character.toLowerCase(str.charAt(i));

                             else

                                            s1=s1+Character.toUpperCase(str.charAt(i));

              }

              System.out.println("Toggle String : "+s1);

               

  }          

}

 

Python

str1 = input("Enter a string: ")

print("Toggled String : ",str1.swapcase())


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !