0% found this document useful (0 votes)
6 views

13. Write a C++ Program to Toggle Each Character in a String 

a

Uploaded by

Ajay kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

13. Write a C++ Program to Toggle Each Character in a String 

a

Uploaded by

Ajay kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

13.

Write a C++ Program to Toggle Each


Character in a String
Program to Toggle each character in a
string
Description about Program to Toggle each character in a string

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 to Toggle each character in a string

#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 to Toggle each character in a string

#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 Program to Toggle each character in a string

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 Program to Toggle each character in a string

str1 = input("Enter a string: ")

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

You might also like