Program to toggle each character in a string | Faceprep

Program to toggle each character in a string is discussed here. A string is given as input in which all the lower case letters are converted to upper case and the upper case letters are converted to lower case.

For example, consider the following string

Input: FacePrep
Output: fACEpREP

Input: FocucAcadeMy
Output: fOCUSaCADEmy

Algorithm to toggle each character in a string

  • Input the string from the user.
  • Traverse the string character by character.
  • If the scanned character is upper case, convert it to lower case.
  • If the scanned character is lower case, convert it to upper case.

Program to toggle each character in a string is given below.

// C program to toggle each character in a string

#include <stdio.h>
#define MAX_SIZE 100

void toggleCase(char * str);

int main()
{
char str[MAX_SIZE];
printf(“\nEnter the string : “);
scanf(“%[^\n]s”, &str);
toggleCase(str);
printf(“\nReplaced string after toggling characters : “);
printf(“%s\n”, str);
return 0;
}

void toggleCase(char * str)
{
while(*str)
{
if(*str >= ‘a’ && *str <= ‘z’)
*str = *str – 32;
else if(*str >= ‘A’ && *str <= ‘Z’)
*str = *str + 32;
str++;
}
}

// C++ program to toggle each character in a string

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX_SIZE 100

void toggleCase(char * str);

int main()
{
char str[MAX_SIZE];
cout << “\nEnter the string : “;
scanf(“%[^\n]s”, &str);
toggleCase(str);
cout << “\nReplaced string after toggling characters : “;
cout << str << endl;
return 0;
}

void toggleCase(char * str)
{
while(*str)
{
if(*str >= ‘a’ && *str <= ‘z’)
*str = *str – 32;
else if(*str >= ‘A’ && *str <= ‘Z’)
*str = *str + 32;
str++;
}
}

// Java program to toggle each character in a string

import java.util.*;

class Main
{
public static void main(String args[])
{
int c;
String str;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the string : “);
str=sc.nextLine();
char a[]=str.toCharArray();
for(c=0;c<a.length;c++)
{
if(a[c]>=’A’ && a[c]<=’Z’)
{
a[c]=(char)((int)a[c]+32);
}
else if(a[c]>=’a’ && a[c]<=’z’)
{
a[c]=(char)((int)a[c]-32);
}
}
System.out.println(“String after characters are toggled : “);
for(c=0;c<a.length;c++)
System.out.print(a[c]);
}
}

# Python program to toggle each character in a string

string = input(“Enter the string : “)

string1 = ”

for i in range(len(string)):
if(string[i] >= ‘a’ and string[i] <= ‘z’):
string1 = string1 + chr((ord(string[i]) – 32))
elif(string[i] >= ‘A’ and string[i] <= ‘Z’):
string1 = string1 + chr((ord(string[i]) + 32))
else:
string1 = string1 + string[i]

print(“String after the characters are toggled :”, string1)

Time complexity: O(n)

Program to toggle each character in a string using standard library functions is given below.

// C++ program to toggle each character in a string

#include
#include
#include
using namespace std;
int main ()
{
char str[50];
cout << “Enter the string : “;
gets(str);
for (int i = 0; str[i] != ‘\0’; i++)
{
if (isalpha(str[i]))
{
if (islower(str[i]))
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
}
}
cout << “String after toggling each characters : ” << str;
}

# Python program to toggle each character in a string

str = input(“Enter a string : “)
print(“String after the characters are toggled : “,str.swapcase())

Output: 

toggle each character in a string