You are on page 1of 7

Lab-08

CF Zero Semester

Practice the following concepts in this lab 
1. Character Input 
2. Character Array 
 
 
 
  Character Input 
 
1. Single Character Input 
2. Multiple Character Input (String) 
 
Single Character input can be obtained using simple scanf function with %c format specifier, there are other ways 
to get single character input, the following example demonstrate how to initialize a character variable, how to get 
character input using scanf function and how to display the values of character variables. 
 
 
Example‐02
#include<Stdio.h>
#include<conio.h>
int main(){
char ch1,ch2;

//Displaying characters directly


Printf(“The first letter of this sentence is %c”,’T’);

//initializing variables of type character


Ch2=’Z’;
Printf(“\nThe last alphabet of English language is %c”,ch2);

//User input for character variables, type only one character and press
enter
printf(“\nEnterfirst letter of your name %c : ”,ch1);
scanf(“%c”,&ch1);

printf(“\nYour name starts with letter %c “,ch1);

getch();
return 0;
}

CFP-Lab prepared by NaumanShamim


User input using getch() and getche() function 
 
getch() and getche() are library functions, both are used to get single key input from the user, the input is always 
taken as character. The getch() function get single key input from the user but as the user presses the key the key 
is not displayed on the console (screen) however in case of getche() (getch with echo) the typed by user is also 
displayed on the screen. 
 
 Example‐03
 
 
#include<Stdio.h>
#include<conio.h>
int main(){

charch;
printf(“Press any key(A-Z or a-z or 0-9) from the keyboard :”);

//in this line the getche() will wait for the input like scanf, as the key is
//pressed the input will be stored in variable ch1, the getch() do not
//require user to press Enter button

ch1=getche();
printf(“\nYou pressed %c key“,ch1);

return 0 ;
}
 
Example‐04

#include<Stdio.h>
#include<conio.h>
int main(){
 
char ch=’a’; 
int count=0; 
printf(“Type 1 to terminate the program”); 
 
do{ 
 
printf(“\nType a key =”); 
ch=getche(); 
 
if(ch==’a’||ch==’A’||ch==’e’||ch==’E’||ch==’I’||ch==’I’||ch==’o’||ch==’O’||ch==’u’||ch==’U’) 
count++; 
 
}while(ch!=’1’); 
 

CFP-Lab prepared by NaumanShamim


printf(“\n Vowel count = %d”, count); 
getch(); 
return 0 ; 

 
Activity‐01 
Modify example‐03 and write a program to count number of digit keys (0 to 9) pressed by the user. 
 
 Example‐05

#include<Stdio.h>
#include<conio.h>
int main(){
 
char ch1,ch2; 
ch1=’A’; 
printf(“Value of ch1= %c\n”,ch1); 
 
ch2=ch1;  //assignment of value stored in ch1 to ch2 
printf(“Value of ch2= %c\n”,ch2); 
 
ch1++; //this will print the character next to the value stored in ch1, more operations possible coming in next labs 
   
printf(“Value of ch1= %c\n”,ch1); 
 
while 
 
getch(); 
return 0; 

In C language characters have numeric values, for example  
char ch=’A’; 
printf(“ Numeric value of variable ch = %d”,ch);  //Note we are printing character as integer, this will give numeric 
//value of the character stored in character variable 
Output: Numeric value of variable ch=65  //See ASCII table, the numeric value of ‘A’ = 65 
If an arithmetic operation is applied on a character or character variable, the operation is performed on the 
numeric values of these characters for example. 
int diff; 
diff=’a’‐‘A’        //’a’ =97, ‘A’=65 in ASCII table, ‘a’‐‘A’ = 97‐65 
printf(“Numeric difference between a and A = %d”,diff); 
Output: Numeric difference between a and A = 32 
 
 
 

CFP-Lab prepared by NaumanShamim


 
 
Example‐06 
 
#include<stdio.h>
#include<conio.h>
void main(){
char ch=’A’;

//displaying character having numeric value 67 in ASCII table 
printf(“67 in ASCII table corresponds to =%c”,67);

//Displaying character and numeric value of a character 
printf(“ch = %c, Numeric value of ch=%d”,ch,ch); //Numeric value of ch=65 

//Applying arithmetic operation on character 
ch=ch+1; //Numeric value of ch=66
printf(“\nch = %c, Numeric value of ch=%d”,ch,ch);
                               //66 corresponds to B in table 
ch=ch+32; //  66 + 32 =98 , 98 corresponds to b in table 
printf(“\nNumeric value of ch+32=%d”,ch);
printf(“\nCharacter value of ch+32=%c”,ch);
getch();
}
 
If characters have numeric values can we compare them as numbers? 
Yes we can compare characters just like we can compare numbers, see example  
 Example‐07 

#include<stdio.h>
#include<conio.h>
void main(){
char ch1=’h’; //numeric value =104
char ch2=’1’; //numeric value =49
printf(“Comparing ch1 and ch2\n”);
if(ch1>ch2) //Numeric comparison 104>49
printf(“Ch1 is greater than ch2\n”);
else
printf(“Ch2 is greater than ch1\n”);

getch();
}

CFP-Lab prepared by NaumanShamim


Task‐01(Hint: Use range method as in case of numbers, A>10 && A<30 to check if A is between 10 and 30)  
Write a program that ask user to enter a character, the program should check that the character entered by user is 
1) a digit 2) small letter 3) capital letter) 
 
Sample output‐01 
Enter a character : A 
Type : Capital letter 
 
Sample output‐02 
Enter a character: 1 
Type: digit 
 
Task‐02 (Hint adding 32 to capital letters will make them small letters) 
Write a program that gets character input from the user, the program should do the following 
a) Convert a small letter to a capital letter 
b) Convert a capital letter to a small letter 
c) Should leave the letter untouched if it is not a letter (not a‐z or A‐Z) 
 
Task‐03  
Write a program that allow user to enter as many character as he/she likes, the program should then display the 
following statistics 
1)Total number of characters typed 
2)How many characters were small letters 
3)How many characters were capital letters 
4)How many characters were digits 
5)How many characters were other than digits and letters 
 
 
Character Arrays 
 
Character Arrays 
In C, a string of characters is stored in successive elements of a character array and terminated by the NULL 
character. For example, the string "Hello" is stored in a character array, msg[], as follows:  

charmsg[6];
msg[0] = 'H';
msg[1] = 'e';
msg[2] = 'l';
msg[3] = 'l';
msg[4] = 'o';
msg[5] = '\0';

CFP-Lab prepared by NaumanShamim


T
The NULL cha racter is writtten using thee escape sequ
uence '0'. Whiile doing operations such aas printing ch
haracter 
a
arrays, the pr intf or puts fu
unction takess the name off character array and startss printing arraay elements ffrom 
e
element 0 till a null character occurs. 
Initializing Ch
haracter arrayys 
C
Can be initiali zed using a list, or string vvalue. 
c
char countr
ry[9]={'P'
','a','k','
'i’,’s’,’t
t’,’a’,’n’,
,’\0’}; //
/size = 8 characters
c s + null
c
char countr
ry[]={'P','a','k','i
i’,’s’,’t’,’a’,’n’,’
’\0’};
c
char countr
ry[8]="Pak
kistan"; //
/No need for
f a null character
r
c
char countr
ry[]="Paks
sitan"; //
/No need for
f a null character
r

P
Printing Chara
acter Arrays 
Character arraays can be printed like inteeger or float aarrays i.e. using a loop and
C d printing eacch element, in
n addition 
t
to this charac ter arrays can
n be printed b
by just using n name of the aarrays withou ut []. The form
mat specifier %
%s is used 
in printf for ch
haracter arrayys.  
 
E
Example 
#include<S
# Stdio.h>
#
#include<c conio.h>
i
int main() ){
p
printf(“%c”,m
msg[0]) // will print the firstt element of tthe array msgg 
p
printf(“%s”,m
msg) // will print the whole array 
}

LLibrary Funcctions gets() and puts() 
 For sttring input 
 gets(Nam me of the arraay) 
 For sttring outputt 
 puts(Nam me of the array) 
 puts(“Anyything in thee double quo
otes”) 
E
Example 
#
#include< <stdio.h> >
#
#include< <conio.h> >
v
void mainn(){
c
char namee[10],add dress[30] ];
p
puts(“Ent ter name” ”); //pri inting connstant string
g
gets(name e); //inpput into array
a as string
p
puts(“Ent ter addre ess”);
p
puts(“Dat ta entere ed is”);
p
puts(name e); //priint the arrray
p
puts(addr ress);
}
 

C
CFP-Lab preepared by NaaumanSham
mim
 
 
Task‐01 
Write a program that asks user to enter his name and roll number as string, store the values in a 
character array, try to get the input using a single scanf (the values may not be scanned correctly), 
modify the program to get the input using gets. Display all the messages for user using puts, print the 
array using puts 
 
Task‐02 
Write a program that uses gets to get a string from user and stores it in a character array. You need to 
print the array in reverse. The task can be done in two ways, store the reverse of the array in another 
array (you may name it reverse) and display this array or use loop to print the character array in reverse. 
 
Task‐03 
Write a program that initializes a character array using a string from the user, the program should print 
the length of the string, first character of the string and last character of the string (not the final null 
character)  
 
Task‐04 
Modify task‐03 allow the user to type an address line that should have some digits also such as street 
number or house number etc. the program should print the number of character and digits in the string 
provided by the user 
Task‐05 (Get Substring) 
Write a program that lets the user enter a line of text, the program should then ask user the length and 
starting point of substring from the this line, if valid inputs are provided the program should print the 
sub‐string. Sample output of the program is as under. 
 
Sample Output 
Enter a line : Today the sky is blue 
Substring from: 7 
Substring length: 7 
Substring : the sky 
 
Task‐06 
Write a program that gets a line of text consisting of multiple words all in small case, the program should 
offer following choices to the user. 
 
1‐Sentence case (this should make first letter of the line capital if not already) 
2‐All caps (All characters of all lines should be turned to capital letters) 
3‐Words Caps (First letter of each word in the string should be a capital letter) 
4‐Word Count (The number of words in the line) 

CFP-Lab prepared by NaumanShamim

You might also like