Professional Documents
Culture Documents
Lab 06 PDF
Lab 06 PDF
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;
//User input for character variables, type only one character and press
enter
printf(“\nEnterfirst letter of your name %c : ”,ch1);
scanf(“%c”,&ch1);
getch();
return 0;
}
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’);
#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
//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();
}
charmsg[6];
msg[0] = 'H';
msg[1] = 'e';
msg[2] = 'l';
msg[3] = 'l';
msg[4] = 'o';
msg[5] = '\0';
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)