Thursday, 23 May 2013

CONVERT LOWER CASE SENTENCES INTO UPPERCASE USING SIMPLE C PROGRAM

In this post I will be explaining the real use of getch() function in C.
Most of us know about getch() function in C, but we don't know it's real use. getch() function is used to get a character from console, and the most interesting thing that I like about this function is that, this function can even be used to get blank spaces and tabs from the console. You must be acquainted with the scanf() function which can be used to get string through %s format specifier, but do you know that if you wist to get your full name through scanf(), you will find that the scanf() function just takes the first string (string before blank space) as input and ignores the second string. So what if you wish to input your full name (with blank within your first name and last name)? The most simple solution to this query is getch() function.
The following program clearly illustrates the use of getch() function.
In the following program, I have converted the lower case sentence inputted by user into upper case. For this conversion I have used the ASCII codes of each alphabet in the string.
Another interesting thing about this program is that, in this what ever user inputs, other that lower case alphabets, those special characters are echoed to the screen as the are, without any change. This may be a logical error for most of the other ones.

Source Code :-
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *str;
int i=0,p,length=0,character;
clrscr();
do
{
str[i]=getch();
 if(str[i]==13)
break;
length++;
printf("%c",str[i]);
i++;

}while(1);

printf("\n");
for(i=0;i<length;i++)
 {
if((int)str[i]<=122&&(int)str[i]>=97)
{

character=(int)str[i]-32;
printf("%c",character);
}
else
{
printf("%c",str[i]);
}

 }
getch();
}
/******************************************************************************/


No comments:

Post a Comment