0% found this document useful (0 votes)
9 views8 pages

Essential C String Functions Explained

The document provides an overview of built-in string functions in C, specifically those defined in the string.h header file. Key functions include strcat() for concatenating strings, strcmp() for comparing strings, strcpy() for copying strings, strlen() for measuring string length, strrev() for reversing strings, and strstr() for searching substrings. Each function is explained with its syntax, usage examples, and sample output.

Uploaded by

n9886082
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Essential C String Functions Explained

The document provides an overview of built-in string functions in C, specifically those defined in the string.h header file. Key functions include strcat() for concatenating strings, strcmp() for comparing strings, strcpy() for copying strings, strlen() for measuring string length, strrev() for reversing strings, and strstr() for searching substrings. Each function is explained with its syntax, usage examples, and sample output.

Uploaded by

n9886082
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Built-in String Functions:

 C library supports a large number of built-in string functions in a header


file called string.h
 The most important string handling functions are tabulated as follows:
strcat() Concatenates two strings
strcmp() Compares two strings
strcpy( ) Copies one string to another
strlen( ) Finds the length of a string
strrev() Reverses a string

1)strcat() Function:
 This function is used to concatenate or join 2 strings together.
 It takes the following form:
strcat(string1,string2);
where string1 and string2 are character arrays.
 When this function is executed string2 is appended(joined) to string1.
 It does by removing the null character at the end of string1 and placing
string2 from [Link] contents of string2 remain unchanged.
 The size of string1 must be large enough to hold the final string.
For Eg:
word1:
V E R Y \0
word2:
G O O D \0

strcat(word1,word2);
The execution of the above statement will result as follows:
word1:
V E R Y G O O D \0
word2:
G O O D \0

The above statement can be also written as:


strcat(word1,”GOOD”);
Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20]="Sri";
char str2[15]="nivas";
printf("First String is %s \n",str1);
printf("Second String is %s\n",str2);
strcat(str1," ");
strcat(str1,str2);
printf("Resultant String is: %s",str1);
}
Output:
First String is Sri
Second String is nivas
Resultant String is: Sri nivas

2)strcmp() Function:
 This function compares two strings identified by arguments and has value
0 if they are equal.
 It takes the following form
strcmp(string1,string2);
where string1 and string2 may be string variables or string constants.
For Eg: name1=Rajesh;
name2=Harish;
strcmp(name1,name2);
This statement returns non-zero value because the strings are not equal.
strcmp(name1,”Rajesh”);
This statement returns zero because the strings equal.
strcmp(“RAM”,”ROM”);
This statement returns non-zero value because the strings are not equal.
Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20],s2[20];
int value;
printf("Enter First string");
gets(s1);
printf("\nEnter Second String");
gets(s2);
value = strcmp(s1, s2);
if(value==0)
{
printf("\n Strings are not equal”);
}
else
printf("\n Strings are equal”);
}
Output:
Enter First string
Mangalore
Enter Second String
Bangalore
Strings are not equal

3)strcpy() function:
 This function works almost like a string-assignment operator.
 The general format of this function is
strcpy(string1,string2);
 The above statement assigns the content of string2 to string1. String2 may
be a string variable or a string constant.
For Eg: strcpy(city, “DELHI”);
This statement will assign the string “DELHI” to string variable city.
If city1= “DELHI”;
city2= “CHENNAI”;
strcpy(city1,city2);
The above statement will assign the contents of string variable city2 to
[Link] string city1 will contain “CHENNAI” now.
The size of array city1 should be large enough to contain the contents of city2.
Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[20]="Computer Science";
char s2[20]= "bca";
char s3[20];
printf("First String is: %s\n",s1);
printf("\nSecond String is: %s",s2);
strcpy(s3,s1);
printf("\nResultant String is: %s",s3);
}
Output:
First String is: Computer Science
Second String is: bca
Resultant String is: Computer Science

4)strlen() Function:
 This function counts and returns the number of characters in the specified
string.
 The general format of this function is:
n = strlen(string);
where n is an integer variable which receives the value of the length of
the string.
 The argument in this function can be a string constant also.
For Eg: n=strlen(“MANGALORE”);
The above statement counts the number of characters in the given string and
is assigned to n.
i.e. 9 is assigned to n.
int m;
char city[] = “DELHI ”;
m = strlen(city);
The above statement assigns 5 to the variable m.
 strlen() function does not include the null character in the count.
Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[15],s2[20];
int l1,l2;
printf("\nEnter First String:");
gets(s2);
printf("\nEnter Second String:");
gets(s1);
l1 = strlen(s1);
l2 = strlen(s2);
printf("\nNumber of Characters in %s = %d\n",s1,l1);
printf("\nNumber of Characters in %s = %d\n",s2,l2);
}
Output:
Enter First String: September
Enter Second String: June
Number of Characters in September = 9
Number of Characters in June = 4

5)strrev() Function:
 This function can be used to reverse a string.
 The reversed string is stored in the same string.i.e, strrev() does not create
a new string,it reverses and replaces the original string itself.
 The general form of this function is:
strrev(string);

Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
printf(“Enter a string to reverse\n”);
gets(str);
strrev(str);
printf(“Reverse of entered string is %s”, str);
}
Output:
Enter a string to reverse
Mouse
Reverse of entered string is esuoM

6)strstr() Function:
 This function stands for string search.
 This function is used to find the first occurrence of a substring in a given
string.
 If the substring is not present it returns a null value.
 The general form of this function is:
strstr(string,match);
Here,string is the input string in which the substring should be found.
match is the substring we want to search in the given string.

Example Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50]=”C Programming and JAVA Programming”;
char sub[20];
sub=strstr(str,”Programming”);
if(sub!=NULL)
{
printf(“Substring is Found”);
}
else
{
printf(“Substring is not found”);
}
getch();
}

Output:
Substring is Found

You might also like