Write a C Program that input any Small letter and display it with capital letter

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

Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment