How to Convert Character From Lowercase to Uppercase in C

In C programming language there are some excellent library function for processing character data. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of character divided into lines; each line consists of zero or more characters followed by a newline character. The standard library provides several functions for reading or writing one character at a time and we show you the usability some of this function
It is very much easy to convert a character from lowercase to uppercase using C programming language. There are some library function which makes this more easier and we discuss this here in details. Here is a complete C program that reads in a lowercase character, converts it to uppercase and then display the uppercase equivalent.


#include <stdio.h>;
#include <ctype.h>;

int main()
{
      int lower, upper;

      lower = getchar();
      upper = toupper(lower);
      putchar(upper);

      return 0;
}

This program contains three library functions: getchar(), toupper() and putchar().

  • getchar() – returns a character that is entered from the keyboard.
  • toupper() – returns the uppercase equivalent of its argument.
  • putchar() – causes the value of the argument to be displayed.

Notice that the last two functions each have one argument but the first function does not have any arguments, as indicated by the empty parentheses.

Also notice that preprocessor statements which is highlighted appear at the start of the program. These statements cause the contents of the header file stdio.h and ctype.h to inserted into the program the compilation process begins. The information contains in these files is essential for the proper functioning of the library functions getchar(), putchar() and toupper().

I explain this program in details for the beginners to understand easily.

Now we write the same program without library function so that we can understand these library function more in depth. Hence, you should concentrate on the overall logic, and not worry about the details of each individual statement just yet.

Here is the complete program,


#include <stdio.h>;

int main()
{
      char lower, upper;

      printf("Please input a lowercase character: ");
      scanf("%c", &lower);

      if(lower >= 'a' && lower <= 'z'){
            upper = ('A' + lower - 'a');
      }
      else{
            upper = lower;
      }

      printf("nThe uppercase equivalent is: %cn", upper);
      return 0;
}

You can also write this program using separate function like below


#include <stdio.h>;

char lower_to_upper(char ch1)
{
      char ch2;

      if(ch1 >= 'a' && ch1 <= 'z'){
            ch2 = ('A' + ch1 - 'a');
            return ch2;
      }
      else{
            ch2 = ch1;
            return ch2;
      }
}

int main()
{
      char lower, upper;

      printf("Please input a lowercase character: ");
      scanf("%c", &lower);

      upper = lower_to_upper(lower);

      printf("nThe uppercase equivalent is: %cn", upper);
      return 0;
}

Here lower_to_upper() function carries out the actual character conversion. This function converts only lowercase letters; all other characters are returned intact. A lowercase letter is transferred into the function via the argument c1, and the uppercase equivalent , c2, is returned to the calling portion of the program ( to main) via the return statement.

Now consider the main function, which follows lower_to_upper(). This function reads in a character which may or may not be a lowercase letter and assigns it to the char-type variable lower. function main then call the function lower_to_upper(), transferring the lowercase character (lower) to lower_to_upper(), and receiving the equivalent uppercase character (upper) from lower_to_upper(). Notice that the variables lower and upper in main corresponds to the variables c1 and c2 within lower_to_upper().

I think you can understand this program. The highlighted part of the code above is the logical part. Here we use the ASCII value of each character the ASCII value of ‘a’ is 97 and ‘z’ is 122. Say we input ‘m’ as input whose ASCII value is 109, need to convert it into ‘M’ so put the value of these character into this logical line (‘A’ + lower – ‘a’). here lower=109 (ASCII value of ‘m’) so we get (65 + 109 – 97) = 77, which is the ASCII value of ‘M’ and thus the output will be ‘M’.

You will find here all the ASCII Character Set .

About Md. Moin Uddin

I love to research on new technology and try to play with those. It makes my work really enjoyable.

8 thoughts on “How to Convert Character From Lowercase to Uppercase in C”

  1. pls discuss a c progarm to find a longest word in a given string…pls send answer in my mailid as soon as posible..thank u…

  2. #include
    #include
    int main()
    {
    FILE *fd;
    fd=NULL;
    char fname[10],gtr[10],ch;

    char buff[1024];
    int i,marks[10],j;
    printf(“Enter .txt file name\n”);
    gets(fname);
    fd=fopen(fname,”w”);
    if(fd!=NULL)

    { printf(“enter your statment\n”);
    gets(gtr);
    puts(gtr);
    printf(“for lower case press ‘l’\n”);
    ch=getche();
    switch(ch){
    case(‘l’):{
    {for(j=0;i<sizeof(gtr);i++)
    {for(i=65;i<=90;i++)
    if(gtr[j]=i)
    {

    gtr[j]=i+32;

    }
    }
    }
    for(j=0;i<sizeof(gtr);i++)
    {
    printf("%c",gtr[j]);
    }
    }
    break;
    printf("for lower case press 'u'\n");
    case('u'):
    {
    {for(j=0;i<sizeof(gtr);i++)
    {for(i=97;i<=122;i++)
    if(gtr[j]=i)
    {

    gtr[j]=i-32;

    }
    }

    }
    for(j=0;i<sizeof(gtr);i++)
    {printf("%c",gtr[j]);
    }
    }
    }
    }
    else
    {
    fprintf(fd,"error");
    }
    fclose(fd);
    }

  3. I have very easy way to convert Lowercase to Uppercase.
    char lowercase_m=’a’;
    printf(“Uppercase is: %c”,lowercase_m-’a’+’A’);
    getch();
    Hope this help.

  4. this program for only one word this program con`t convert a sentence,
    suppose :input :atiqur rahman
    output:atiqur
    incomplete output ,
    i need a complete output,
    THANKS ALL

Leave a Reply

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

one × two =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>