C

Convert a String to Uppercase in C Without toupper

In this tutorial, we are going to see how to convert a string to uppercase in C without toupper.
 

Convert a string to uppercase in C Without toupper
#include <stdio.h>
#include <string.h>

int main() {
   char str[100];
   int i;
   printf("\nEnter the string to convert to uppercase: ");
   gets(str);
   for (i = 0; str[i]!='\0'; i++) {
     /* if the characters are in lower case, convert them to
        upper case by subtracting 32 from their ASCII value. */
      if(str[i] >= 'a' && str[i] <= 'z') {
         str[i] = str[i] -32;
      }
   }
   printf("\nThe string in upper case = %s", str);
   return 0;
}

Output:

Enter the string to convert to uppercase: welcome to stackhowto!                         

The string in upper case = WELCOME TO STACKHOWTO!

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *