A little more of 'C'




1. getchar()
- to input a single character

SYNTAX: variable_name= getchar();


2- putchar()
- to display a single character

SYNTAX: putchar(variable_name);

Examples:

1) To illustrate the syntax of the functions given above.

#include <stdio.h>
#include <conio.h>
void main()
{ char a;
printf("Enter your gender\n");
a=getchar();
printf("Gender:\n");
putchar(a);
getch();
}

Note: Some notations can be used in programming languages for certain operations with a backslash.
These are known as ESCAPE SEQUENCES. Some of their examples are:
\n    newline feed
\t     horizontal tab
\b    backspace
\\     backslash
\'      apostrophe
\v     vertical tab

Likewise various escape sequences can be used in all the programming languages notifying various visible and invisible characters.

The ASCII CODE for various alphabetic and numeric characters used in programming are:
 A-Z     65-90
 a-z       97-122
 0-9       48-57
 space    32


P1. WAP to convert a lower case charater to upper or vice versa depending upon the users choice.

#include <stdio.h>
#include <conio.h>
void main()
{ char a, int p;
printf("Press 1 to convert from lower to upper OR 2 to convert from upper to lower\n");
scanf("%d",&p);
switch(p)
{case 1: printf("Enter any lowercase character\n");
a=getchar();
if(a<=122&&a>=97)
{a-=32;
putchar(a);}
else
printf("Entered character is not a lowercase character\n");
break;
case 2: printf("Enter any uppercase character\n");
a=getchar();
if(a<=90&&a>=65)
{a+=32;
putchar(a);}
else
printf("Entered character is not an uppercase character\n");
break;
default: printf("Entered choice is wrong\n");
}
getch();
}

Notes:
1. switch-case is a multi-branching conditional statement which matches the case depending upon the value of variable passed as an argument in switch and executes the statements in the matching case.
"default" case executes when the value of the passed argument does not match with any of the given cases.

2. break is a jumping statement which upon execution takes the control out of the block of statements it is contained in. As if in switch-case, break statement is used to avoid fall-through. Fall-through can be defined as the execution of the following cases of the matching case, and the control finally transfers to the following statement of the switch block.

0 comments: