Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write programs to read character and convert it into upper case if it is in lower and vice-versa.

Object II. To write programs to read string and convert it into upper case if it is in lower and vice-versa.

Download this full program | Lab 8 | in pdf format

Background Theory:

Reading & Writing a character: The simplest of all input/output operations is reading a character from the standard input unit and writing it to the standard output unit. Reading a single character can be done by using the function getchar. Like getchar, there is an analogous function putchar for writing characters one at a time to the terminal.

variable_name = getchar( );

putchar(variable_name);

ASCII Values: Most computers make use of American Standard Code for Information Interchange character set, in which each individual character is numerically encoded with its own unique 7-bit combination (hence a total of 27= 128 different characters). Upper case characters count onward A 65 to Z 90 and lower ones from a 97 to 122. Function toascii checks the ASCII value the character entered.

String handling: A string is an array of characters. Any group of characters (except double quote sign) defined between double quotations marks is a constant string. The familiar input function scanf can be used with %s format specification to read in a string of characters but the problem with the scanf function is that it terminates its input on the white space (blanks, tabs, new lines, carriage returns, form feed) it finds. Also the ampersand & is not required before the variable name in strings.

 The gets and puts functions offer simple alternatives to the use of scanf and printf for reading and displaying strings. Function ctype.h must be included to work with characters and strings. 

 

Code:

//program to change character case//

#i #include <stdio.h>

#include <conio.h> 

#include <ctype.h>

void main()

{

            int val;

            char c, d;

            clrscr();

            printf("Enter a character: ");

            c=getchar();                // alternatingly scanf("%c", &a) and val=toascii(a)//

            printf("You entered: ");

            d= putchar(c);

 

            val = toascii(c);

            printf("\n\nThe corresponding ASCII value is %d.", val);

 

            if(val>=97 && val<=122)

            {

                        printf("\nYour character %c is lower cased.", d);

                        val-=32;

                        printf("\nUpper cased: %c", val);

            }

 

            else if(val>=65 && val<=90)

            {

                        printf("\nYour character %c is upper cased.", d);

                        val+=32;

                        printf("\nLower cased: %c", val);

            }

            getch();

}

To Download this full program | Lab 8 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com