Search C Program | nimishsoft@gmail.com

String to Upper Case - STRUPR

/* STRING TO UPPER CASE - STRUPR */
#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;
 }
 
 printf("String in upper case is: %s",str);

 return 0;
}

4 comments:

  1. please can you tell me what u did to make the string to uppercase?
    and for what this is used = str[i]-=32; ??

    ReplyDelete
    Replies
    1. ASCII values for a is 97 and A is 65.
      97-65=32

      Delete