C program to convert the string into toggle

Toggle means converting small letters into capital letter and capital letter into small letter.
Example:
Let the given string be " InterviewCodingPrep " then the output should be in toggle from that is "iNTERVIEWcODINGpREP"
Input: InterviewCodingPrep
Output: iNTERVIEWcODINGpREP

Example:
Input: StRing
Output: sTrING

Input: ProgRammIng
Output: pROGrAMMiNG

The source code for the above explanation is given below:

Program:



int main()
{
char s[30];
int i;   
clrscr(); 
puts("Enter the string:")
gets(s);
for (i=0;s[i]='\0';i++)
    {
       if(s[i]>=65 && s[i] <=90)
          s[i] += 32;
       else if(s[i]>=97 && s[i]<=122)
          s[i] -= 32;
      }
printf("\n String in toggle case: %s", s);
return 0;
}

Output:

Enter the string: InterviewCodingPrep
String in toggle case: iNTERVIEWcODINGpREP

In the above program first main() is declared as any program should start with main(). A variable 's' is declared with size 30 as an character ( char is the representation ), an variable 'i' is declared as an integer ( int ), puts() works same as printf() function it will display the given infromaton on the screen and gets() is used to take the given data from the user. Logic for toggle is executed here the range of small letters are from 95 to 122 and the range of capital letters are from 65 to 90, so by subtracting 32 to the small letter gives the capital letter and by adding 32 to the capital letter gives the small letter. Finally, the result is displayed on the display screen.


Post a Comment

0 Comments