Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lecture 23 - Strings & String Handling Functions

Uploaded by

Zahid Wani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 23 - Strings & String Handling Functions

Uploaded by

Zahid Wani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

STRINGS& STRING HANDLING FUNCTIONS

TABLE OF CONTENTS
=======================

STRINGS & STRING HANDLING FUNCTIONS.........................................................................................................1


PROGRAMS TO P ERFORM V ARIOUS O PERATIONS ON S TRINGS WITHOUT U SING STRING H ANDLING
FUNCTIONS (CONTINUED )..........................................................................................................................................2
4. PROGRAM TO COPY ONE STRING INTO STRING..........................................................................................................2

PROGRAMS PERFORM VARIOUS OPERATIONS ON STRINGS USING VARIOUS STRING HANDLING


TO
FUNCTIONS....................................................................................................................................................................3

1. PROGRAM TO FIND LENGTH OF A STRING USING strlen() FUNCTION......................................................................3


2. PROGRAM TO CONCATENATE TWO STRINGS USING strcat() FUNCTION................................................................4
3. PROGRAM TO COMPARE TWO STRINGS USING strcmp() FUNCTION.......................................................................5
4. PROGRAM TO COPY ONE STRING INTO STRING USING strcpy() FUNCTION..........................................................7

1|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS WITHOUT
USING STRING HANDLING FUNCTIONS (CONTINUED)

4. PROGRAM TO COPY ONE STRING INTO STRING


#include<stdio.h>
#include<conio.h>
void main()
{
char s1[10],s2[10];
int i;
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
i=0;
while(s1[i]!= ‘\0’)
{
s2[i]=s1[i];
i++;
}
s2[i]= ‘\0’;
// 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

2|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS USING
VARIOUS STRING HANDLING FUNCTIONS

1. PROGRAM TO FIND LENGTH OF A STRING USING strlen() FUNCTION

In C programming, the strlen() function returns the length (no of characters ) of a string.

The general syntax of using strlen() function is given as:


strlen(stringname);
E,g; strlen(s1)
This function returns the length of s1
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char abc[20];
int x;
printf("Enter any string\n");
gets(abc);
printf (“You have entered the following string\n”);
puts(abc);
x = strlen(abc);
printf(“The length of string is %d characters \n”, x);
getch();
}

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

In C programming, the strcat() function concatenates (joins) two strings.

The general syntax of using strcat() function is given as:


strcat(destination_string , source_string)
E,g; strcat(s1 ,s2)
Copies s2 at the end of s1 or appends s2 to s1
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
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 logic of concatenating two strings starts from here
strcat(s1,s2);
// The second string has been appended to the first string but itself remained as
it //using the above function
printf(“The first string after concatenation will be now \n ”);
puts (s1);
printf(“The second is like as it was and is given as \n ”);
puts (s2);
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 after concatenation will be now
zahidhussain
The second is like as it was and is given as
hussain

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

4. PROGRAM TO COPY ONE STRING INTO STRING USING strcpy() FUNCTION

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:

char* strcpy(char* dest, const char* src);

dest: Pointer to the destination array where the content is to be copied.

src: string which will be copied.

Return Value: After copying the source string to the destination string, the strcpy() function
returns a pointer to the destination string.

Below program explains different usages of this library function:

#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

You might also like