C program to toggle characters in the string

    Convert the string into toggle

Toggle means converting capital letters into small and small letters into capital.
If the given input has an small letters then the letters should be converted into capital letters and            converts capital letters into small letters

Example:If the given input is 'sTrinG' then the output should convert a small letters into capital and capital letters into small letters i.e, the output should be StRINg.

        INPUT: sTrinG
        OUTPUT: StRINg


       Program:

       /* C program to convert a String into toggle*/

      #include<stdio.h>
      #include<conio.h>   //header files
      int main()
      {
        char s[30];  //size of s is 30
        int i;
        clrscr();    //clear screen
        puts("Enter any string:\n");    
        gets(s);   //takes the given input (string)
        for(i = 0; s[i] = '\0'; i ++)
       {         //logic to toggle a string
        if(s[i] >= 65 && s[i] <= 90)
           s[i] += 32;
        else if(s[i] >= 95 && s[i] <= 122)
            s[i] -= 32;
       }
    printf("String in toggle case :%s",s);
    return 0;
    }


Output 

Enter any string:
sTrinG
String in toggle case :StRINg

Program explanation 

1. stdio and conio are header files which are used for standard input and output
2. #include is used to include the files
3. clrscr() is used to clear the screen
4. puts() is used to display the given text on the display screen
5. gets() is used to take the given input , it is same as scanf()
6. return() is used to return the value to the main() 

No comments:

Post a Comment