C Program to toggle case of a string

Category: C ProgramTags: cprogram string

C program to toggle the case of a string using the loop i.e., convert the uppercase alphabets into lowercase and lowercase alphabets into uppercase. How to transform the case of characters in a string.

C Program to toggle the case of a string

#include <stdio.h>
#include <string.h>

void main()
{
  char str[50];
  int i=0;

  printf("Enter uppercase string: ");
  gets(str);

  while(str[i] != '\0')
  {
    if(str[i]>='a' && str[i]<='z')
    {
      str[i] = str[i] - 32;
    }
    else if(str[i]>='A' && str[i]<='Z')
    {
      str[i] = str[i] + 32;
    }
    i++;
  }

  printf("Togglecase string: %s", str);
}

Output

Output

Enter uppercase string: pRoCoDinG
Togglecase string: PrOcOdINg