Lecture 20-21-22 - Strings or Character Arrays in C
Lecture 20-21-22 - Strings or Character Arrays in C
TABLE OF CONTENTS
=======================
1|Page
STRINGS / A CHARACTER ARRAY IN C:
String is a sequence of characters that is treated as a single data item and terminated by null
character '\0'. Remember that C language does not support strings as a data type. A string is actually
one-dimensional array of characters in C language. These are often used to create meaningful and
readable programs.
For example: The string "hello world" contains 12 characters including '\0' character which is
automatically added by the compiler at the end of the string.
Example: char name [15]; // Declares name a string / character array for 14 characters
Remember that when we initialize a character array by listing all of its characters separately then we
must supply the '\0' character explicitly.
char str[5];
str = "hell"; // Illegal as we can’t put declaration and initialization separately
However, C supports a format specification known as the edit set conversion code %[..] that can be
used to read a line containing a variety of characters, including white spaces.
2|Page
Example:
#include<stdio.h>
#include<string.h>
voidmain()
charstr[20];
printf("Enter a string");
scanf("%[^\n]", &str); //scanning the whole string, including the white spaces
printf("%s", str);
Another method to read character string with white spaces from terminal is by using the
gets() function.e.g
char text[20];
Same way we can use puts () function instead of printf to print the results. e.g
char text[20];
3|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS WITHOUT
USING STRING HANDLING FUNCTIONS
OUTPUT:
Enter any string
Zahid Hussain
You have entered the following string
Zahid Hussain
The length of string is 13 characters
4|Page
2. PROGRAM TO CONCATENATE TWO STRINGS
#include<stdio.h>
#include<conio.h>
void main()
{
Char name1[20],name2[20];
int i, count=0;
printf("Enter first string\n");
gets (name1);
printf("Enter second string\n");
gets (name2);
printf (‚You have entered the following first string\n‛);
puts (name1);
printf (‚You have entered the following second string\n‛);
puts (name2);
i=0;
while(name1[i]!= ‘\0’)
{
count = count + 1;
i++;
}
name1[count]=’ ’;
count=count+1;
i=0;
while(name2[i]!= ‘\0’)
{
name1[count]=name2[i];
i++;
count++;
}
name1[count]= ‘\0’
// The second string has been appended to the first string but itself remained as it
printf(‚The first string will be now \n ‛);
puts (name1);
printf(‚The second is like as it was and is given as \n ‛);
puts (name2);
getch();
}
OUTPUT:
Enter first string
Zahid
Enter second string
Hussain
You have entered the following first string
Zahid
You have entered the following second string
Hussain
The first string will be now
Zahid hussain
The second is like as it was and is given as
hussain
5|Page
3. PROGRAM TO COMPARE TWO STRINGS
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
int i, flag = 0;
printf("Enter first string\n");
gets (name1);
printf("Enter second string\n");
gets (name2);
printf (‚You have entered the following first string\n‛);
puts (name1);
printf (‚You have entered the following second string\n‛);
puts (name2);
i=0;
while(name1[i]!= ‘\0’ || name2[i]!= ‘\0’)
{
if (name1[i] != name2 [i])
{
flag = 1;
break;
}
i++;
}
if(flag == 0)
{
printf (‚The strings are Identical‛);
}
else
{
printf (‚The strings are NOT Identical‛);
}
OUTPUT 1:
Enter first string
Zahid
Enter second string
zahid
You have entered the following first string
Zahid
You have entered the following second string
zahid
The strings are Identical
OUTPUT 2:
Enter first string
Zahid
Enter second string
Wani
You have entered the following first string
Zahid
You have entered the following second string
Wani
The strings are NOT Identical
6|Page