Search C Program

Toggle case of string

/* TOGGLE CASE OF STRING */

#include<stdio.h>

int main()
{
 char str[100];
 int i;
 printf("Please enter a string: ");

 // gets(str); 
 // fgets is a better option over gets to read multiword string .

 fgets(str, 100, stdin);
 
 // Following can be added for extra precaution for '\n' character
 // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL;

 for(i=0;str[i]!=NULL;i++)
 {
  if(str[i]>='A'&&str[i]<='Z')
   str[i]+=32;
  else if(str[i]>='a'&&str[i]<='z')
   str[i]-=32;
 }
 
 printf("String in toggle case is: %s",str);

 return 0;
}

10 comments:

  1. Great job n i have been following your .n i want reverse the words of a string like
    input:- i am a boy.
    output:- boy. a am i
    kindly send me the answer of it

    ReplyDelete
    Replies
    1. http://cbasicprogram.blogspot.in/2012/05/string-reverse-strrev.html

      Delete
  2. what is the use of str[i] != NULL....i don't know....

    ReplyDelete
  3. What is code for this program?,if i input SK JAHANGIR ALAM and output wil be S.J,ALAM

    ReplyDelete
  4. Replies
    1. It will increase the ASCII value of the character . As the difference of ASCII value between any lowercase character and uppercase character is 32 ,we add 32 which makes uppercase character to its respective lowercase. A=65, a=97 => A+32='a' .so A became 'a' by adding 32 and vice versa in else part.

      Delete
  5. this code needs lot of memory locations can u plz tell me a fast and reliable code

    ReplyDelete
    Replies
    1. if(str[i]>='A'&&str[i]<='Z')
      str[i]=str[i]+'a'-'A';

      elseif(str[i]>='a'&&str[i]<='z')
      str[i]+'A'-'a';


      Delete