Professional Documents
Culture Documents
1
Example: ASCII character
#include <stdio.h>
void main(void)
{
char A = 'A';
char Z = 'Z';
char a = 'a';
char z = 'z';
printf("\n");
printf("\n Decimal 65 in ASCII represents %c\t",65);
printf("\n Decimal 90 in ASCII represents %c\t",90);
printf("\n Decimal 97 in ASCII represents %c\t",97);
printf("\n Decimal 122 in ASCII represents %c\t",122);
}
2
Sample output
ASCII value for A is 65
ASCII value for Z is 90
ASCII value for a is 97
ASCII value for z is 122
3
Example cont…
Both Examples are same
#include <stdio.h> #include <stdio.h>
void main(void)
{ void main(void)
char ch; {
char ch;
printf("enter a character: ");
scanf("%c", &ch); printf("enter a character: ");
if (ch >= 'A' && ch <= 'Z') scanf("%c", &ch);
{
printf("\ncapital if (ch >= 65 && ch <= (65+25))
letter\n"); {
} printf("\ncapital letter\n");
} }
}
4
Fundamentals of Characters and Strings
• A string in C/C++ is an array of characters ending
with the null character (‘\0’).
5
Fundamentals of Characters and Strings
• A string can also be defined by specifying the
individual characters:
char name[ ] = {‘A’, ‘h’, ‘m’, ‘e’, ‘d’, ‘\0’};
A h m e d \0
6
Fundamentals of Characters and Strings
7
Briefly review about strings :
We can initialize string variables at compile time such as;
A h m e d \0 \0 \0 \0 \0
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
8
Fundamentals of Characters and Strings
If we initialize the following string:
char string [4] = “milk”;
9
Example: string and ‘\0’
#include <stdio.h>
void main(void)
{
char string[] = "I love Pakistan";
count = 0;
}
output:
I love Pakistan The string has 15 characters including space.
10
String Manipulation Functions
Include <string.h> to use these functions.
11
String Comparison Functions
Include <string.h> to use these functions
12
Strcmp()
13
Strcmp() – program
#include <stdio.h>
#include <string.h>
void main()
{
char name[20] = “MUMTAZ";
char nameoft[20];
int correct = 0;
while(correct==0)
{
printf("Enter the name of your teacher in uppercase: ");
gets(nameoft);
if(strcmp(name, nameoft)==0)
{
printf("Correct!\n");
correct = 1;
}
else
printf("Try again: \n");
}
}
14
strcat()
strcat ( dest, src );
15
strcat() – program
/* Concatenating Strings Using strcat */
#include <stdio.h>
#include <string.h>
void main()
{
char string1[50] = “Ahmed ";
char string2[ ] = “Mumtaz";
strcat(string1, string2);
printf("string1: %s\n", string1);
}
Output:
string1: AhmedMumtaz
16
Strcpy() and Strlen()
strcpy is short for string copy.
• It copies the entire contents of src into dest.
• The contents of dest after strcpy will be exactly the
same as src.
17
strcpy() – program
#include <stdio.h>
#include <string.h>
void main()
{
char string1[50] = “University of Gujrat, Sialkot Campus";
char string2[50] = “GUJRAT University”;
strcpy(string1,string2);
printf(“string1: %s\n", string1);
printf(“string2: %s\n", string2);
}
Output :
string1: GUJRAT University
string2: GUJRAT University
18
strlen() – program
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[ ] = "I love Pakistan";
int i,count = 0;
count = strlen(string);
printf("%s The string has %d characters including the space“, string, count);
}
output:
I love Pakistan The string has 15 characters including the whitespace
19