Lecture 23 - Strings & String Handling Functions
Lecture 23 - Strings & String Handling Functions
TABLE OF CONTENTS
=======================
1|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS WITHOUT
USING STRING HANDLING FUNCTIONS (CONTINUED)
2|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS USING
VARIOUS STRING HANDLING FUNCTIONS
In C programming, the strlen() function returns the length (no of characters ) of a string.
OUTPUT:
Enter any string
Zahid Hussain
You have entered the following string
Zahid Hussain
The length of string is 13 characters
3|Page
2. PROGRAM TO CONCATENATE TWO STRINGS USING strcat() FUNCTION
4|Page
3. PROGRAM TO COMPARE TWO STRINGS USING strcmp() FUNCTION
strcmp() is a built-in library function and is declared in <string.h> header file. This function
takes two strings as arguments and compare these two strings lexicographically.
Syntax:
int strcmp(const char *leftStr, const char *rightStr );
In the above prototype, function srtcmp takes two strings as parameters and returns an integer
value based on the comparison of strings.
strcmp() compares the two strings lexicographically means it starts comparison character by
character starting from the first character until the characters in both strings are equal or a NULL
character is encountered.
If first character in both strings are equal, then this function will check the second character, if
this is also equal then it will check the third and so on
This process will be continued until a character in either string is NULL or the characters are
unequal.
This function can return three different integer values based on the comparison:
1) Zero ( 0 ): A value equal to zero when both strings are found to be identical. That is, All
of the characters in both strings are same.
2) Greater than zero ( >0 ): A value greater than zero is returned when the first not
matching character in leftStr have the greater ASCII value than the corresponding
character in rightStr.
3) Less than Zero ( <0 ): A value less than zero is returned when the first not matching
character in leftStr have lesser ASCII value than the corresponding character in rightStr.
5|Page
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
int x;
printf("Enter first string\n");
gets (s1);
printf("Enter second string\n");
gets (s2);
printf (“You have entered the following first string\n”);
puts (s1);
printf (“You have entered the following second string\n”);
puts (s2);
// The below strcmp() function checks whether s1 and s2 are same or not
x = strcmp(s1,s2);
if(x==0)
printf(“Strings are Identical”);
else
printf(“Strings are NOT Identical”);
getch();
}
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
OUTPUT 3:
Enter first string
Zahid
Enter second string
zahidd
You have entered the following first string
Zahid
You have entered the following second string
6|Page
Zahidd
The strings are NOT Identical
strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it
is present in string.h header file.
Syntax:
Return Value: After copying the source string to the destination string, the strcpy() function
returns a pointer to the destination string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
printf("Enter Source string\n");
gets (s1);
printf (“You have entered the following first string\n”);
puts (s1);
//Logic for copying s1 into s2 starts from here
strcpy(s2,s1);
// The destined string s2 after copying will be given as
printf(“The second string s2 after copying s1 is given as \n ”);
puts (s2);
getch();
}
OUTPUT:
Enter Source string
Zahid Hussain Wani
The second string s2 after copying s1 is given as
Zahid Hussain Wani
7|Page