You are on page 1of 19

Fundamentals of Characters and Strings

• In C language characters is either printable or


nonprintable including lowercase letters, uppercase
letters, decimal digits, special characters and escape
sequences.

• A character is usually stored in the computer as an 8-


bits (1 byte) integer.

• The integer value stored for a character depends on


the character set used by the computer on which the
program is running.

1
Example: ASCII character
#include <stdio.h>
void main(void)
{
char A = 'A';
char Z = 'Z';
char a = 'a';
char z = 'z';

printf("\nASCII value for A is %d\t", A);


printf("\nASCII value for Z is %d\t", Z);
printf("\nASCII value for a is %d\t", a);
printf("\nASCII value for z is %d\t", 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

Decimal 65 in ASCII represents A


Decimal 90 in ASCII represents Z
Decimal 97 in ASCII represents a
Decimal 122 in ASCII represents z

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’).

• It is written inside a double quotation mark (“ ”)

• A string may be assigned (in a declaration) to either a


char array or to a char pointer:
char color[] = “green”;
char color[6]={‘g’,’r’,’e’,’e’,’n’,’\o’};
OR
char *color = “green”;

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 string is accessed via a pointer to the first character


in the string.

• In memory, the characters are stored as:

A h m e d \0

6
Fundamentals of Characters and Strings

• For five characters string ‘Ahmed’, six characters are


stored in the memory.

• Every string is terminated with a null character ‘\0’,


which indicates the end of the string.

• For an array of characters to be stored as a string,


the array must be large enough to store the string
plus one terminating NULL character.

7
Briefly review about strings :
We can initialize string variables at compile time such as;

char name[10] = “Ahmed”;


The initialization will be stored in memory 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”;

The system may generate the following syntax


error:
error -------- array bounds overflow

Therefore, we need to declare the array with (the


size of the string + 1) to accommodate the null
terminating character ‘\0’.
char string [5] = “milk”;

9
Example: string and ‘\0’
#include <stdio.h>

/* The program that counts the number of characters in a string */

void main(void)
{
char string[] = "I love Pakistan";

count = 0;

for (int i = 0; string[i] != '\0'; i++)


count++;

printf(“%s The string has %d characters including space", string, count);

}
output:
I love Pakistan The string has 15 characters including space.

10
String Manipulation Functions
Include <string.h> to use these functions.

Functions Description of Functions


strcpy (s1,s2) Copies the string s2 into the array s1.
The value of s1 is returned
strcat (s1,s2) Appends the string s2 to the array s1.
The first character of s2 overwrites the
terminating NULL character of s1. The
value of s1 is returned.

11
String Comparison Functions
Include <string.h> to use these functions

Function Function Description


strcmp (s1, s2) Compares the string s1 to the string s2. The
function returns 0, less than 0 (negative
value), or greater than 0 if s1 is equal to,
less than or greater than s2 respectively.

12
Strcmp()

strcmp() will accept two strings. It will return an integer. This


integer will either be:

• Negative value if s1 is less than s2.


• Zero value if s1 and s2 are equal.
• Positive value if s1 is greater than s2.

Strcmp() is case sensitive.


Strcmp() requires the address of the character array to be
accessed.

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 );

• The strcat is short name for string concatenate,


• It means to add to the end, or append.
• It adds the second string to the first string.
• Make sure that the size of dest is large enough to
hold the entire contents of src and dest string.

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

Defined the string1 array to be large enough to hold the characters


of both strings.

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.

 strlen returns the length of a string, minus the null


character ('\0').

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

You might also like