The 'C' compiler provides the following string handling functions.
STRINGS STANDARD
FUNCTIONS
The
'C' compiler provides the following string handling functions.
Function:
Purpose
strlen():
Used to find the length of the string.
strcpy():
Used to copy one string to another.
strcat():
Used to combine two strings.
strcmp():
Used to compare characters of two strings (difference between small and capital
letters).
strlwr():
Used to convert strings into lower case.
strupr():
Used to convert strings into upper case.
strdup():
Used to duplicate a string.
strrev():
Used to reverse a string.
strncpy():
Used to copy first 'n' characters of one string into another.
strncmp():
Used to compare first 'n' characters of two strings.
strcmpi():
Used to compare two strings without regarding the case.
strnicmp():
Used to compare first 'n' characters of two strings without regarding the case.
stricmp():Compares
two strings (Not difference between small and capital letters).
strchr():Determines
first occurrence of a given character in a string.
strrchr():Determines
last occurrence of a given character in a string.
strstr():Determines
first occurrence of a given string in another string.
strncat():Appends
source string to destination string upto specified length.
strnset():Sets
specified number of characters of string with a given argument or symbol.
strspn():Finds
up to what length two strings are identical.
strpbrk():
Searches the first occurrence of the character in a given string and then it
displays the string starting from that character.
The
commonly used string manipulation functions are as follows:
This
function is used to count and return the number of characters present in a
string.
Syntax:
var =
strlen(string);
Description:
var - Is the integer
variable, which accepts the length of the string.
String - Is the string constant
or string variable, in which the length is going to be found. The counting ends
with first null (\0)char.
Example: Program using strlen()
function.
/* Program using strlen() function */
#include <stdio.h>
#include <conio.h>
main()
{
char name[] =
"MUNI";
int len1,len2;
len1 =
strlen(name); /* To find the length of the string */
len2 =
strlen("VRB");
printf("\n
string length of %s is %d",name,len1);
printf("\n_string
length of %s is %d,", "VRB",len2);
} /* main */
OUTPUT
String length of MUNI
is 4
String length of VRB
is 3
EXPLANATION:
The above program refers to check the length of the string. Here the character
type name[] holds the element 'Muni', it is passed to variable len1. The len2 variable
holds the elements of 'VRB' 'strlen' function detect the length of string len1
and len2 and print the result as above mentioned.
Here,
while calculating the length of the string by using the strlen() the '\0' null
character is not taken into consideration.
This
function is used to copy the contents of one string to another and it almost
works like string assignment operator.
Syntax:
strepy
(string1, string2);
Description:
String1 is the
destination string
String2 is the source
string
The
contents of string2 is assigned to the contents of string1. Where string2 may
be character array variable or string constant.
Example:
char
str[]="MUNI";
char str2[]=
"LAK";
strcpy(str1, str2);
Where
the contents of str2 are copied into the strl and the contents of strl is
replaced with new one.
Example
char str1[10];
strcpy(str1,
"LAK");
Here,
the string constants are copied to the string variable strl.
Example: Program
using strcpy() function
/* Program using strcpy() function */
#include <stdio.h>
main ()
{
char source =
"MUNI"; *Local definitions */
char target[10];
strcpy(target,source); /* string copy */
printf("\n
Source string is %s", source);
printf("\n
Target string is %s", target);
} /* main */
OUTPUT
Source string is MUNI'
Target string is MUNI
EXPLANATION:
In the program the character type variable source contain a string
"MUNI". The variable target[] is declared as a character type. Here
the strcpy() function copy the source string "MUNI" into target
string. Now the variable target also contain the string "MUNI" and
print the variable source and target.
The
strcat() function is used to concatenate or combine, two strings together and
forms a new concatenated string.
Syntax:
strcat(string1,
string2);
Description:
string1
and string2 are character type arrays or string constants.
When
the above strcat() function is
executed, string2 is combined with
string1 and it removes the null character (\0) of string1 and places string2
from there.
Example:
strcat("MUNI”, “LAK”)
Yields MUNILAK
char strl="MUNI”
char str2="LAK"
strcat(str1, str2)
Yields MUNILAK.
Example: Program
using strcat() function
/* Program using strcat() function */
#include <stdio.h>
main()
{
/* Local
definitions */
char
source[]="Ramesh";
char target[10]=
"Babu";
/* Statements */
strcat(source,
target); /* string concatenate function */
printf("\nSource
string is %d", source);
printf("\nTarget
string is %s", target);
} /* main */
OUTPUT
Source string is
Ramesh Babu
Target string is Babu
EXPLANATION:
In the program source[] is a character variable which holds the data
"Ramesh" similarly target[] is a character variable which holds the
data "Babu", The strcat() combines both strings and stores in the
variable "source" finally print the variable "source" and
"target" as mentioned above.
This
is a function which compares two strings to find out whether they are same or
different. The two strings are compared character by character until the end of
one of the string is reached. If the two strings are identical, strcmp()
returns a value zero. If they are not equal, it returns the numeric difference
between the first non‒matching characters.
Syntax:
strcmp(string1, string2);
Description:
string1 and string2 are character type arrays or string constants.
Example: Program
using strcmp() function.
/* Program using strcmp() function */
#include <stdio.h>
#include <conio.h>
main()
{
* Local definitions */
char name[] = "Kalai",
char name[] = "Malai";
int i,j,k;
/* Statements */
i= strcmp(name, "Kalai"); /* string compare function
*/
j= strcmp(namel, name);
k=strcmp(name, "Kalai mani");
printf("\n %d %d %d", i, j, k );
} /* main */
OUTPUT
0 1 6
EXPLANATION:
In the first strcmp(,) the two strings are same, so it returns zero value. In
the second, the first character of the two string is unmatched. So the
difference between ASCII value is printed. In the third one, the strings are
unmatched. The blank space is unmatched with '\0' character. So the ASCII value
difference between '\0' and 'space' is printed.
The
strrev() function is used to reverse a string. This function takes only one
argument and return one argument.
The
general form of strrev() function is,
Syntax:
strrev(string);
Description:
string
are characters type arrays or string constants.
Example: Program
using strrev() function
/* Program using strrev() function */
#include <stdio.h>
main()
{
char y[30]; /*
Local definition */
printf
("Enter the string :");
gets(y);
printf ("The
string reversed is :%s", strrev(y));
} /* main */
INPUT: Enter the string : book
OUTPUT: The string reversed is: koob
EXPLANATION:
The function strrev() is used to print the string in reverse order. The program
gets the input book then reverse that string as koob and prints the result as
output.
The
strchr() function returns a pointer to the first occurrence of the low‒ order
byte of ch in the string pointed to by str. If no match is found, a null
pointer is returned.
Syntax:
char
*strchr(const char *str, int ch);
Description:
Where
Char is string, ch is character
Example: Program
prints the string is a test
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p; c='g'
p =
strchr("C Language", ");
printf(p);
printf("%d",c);
return 0;
}
OUTPUT
C Language
5
The
strlwr() function converts all the uppercase characters in that string to
lowercase characters. The resultant from strtwr() is stored in the same string.
Syntax:
strlwr(string
name);
Description:
Where
string name is string
Example: Program
using strlwr() function
#include <stdio.h>
#include <string.h>
int main()
{
char strl]="LOWER CASE";
puts(strlwr(strl)); //converts to lowercase and displays it.
return 0;
}
OUTPUT
lower case
The
strupr() function converts all the lowercase characters in that string to
uppercase characters. The resultant from strupr() is stored in the same string.
Syntax:
strlwr(string
_name);
Description:
Where
string_name is string
Example: Program
using strupro function
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="upper
case";
puts(strupr(str1));
//converts to lowercase and displays it.
return 0;
}
OUTPUT
UPPER CASE
Computer Programming C: UNIT II: Arrays and Strings : Tag: Computer Science : - C Programming: Strings Standard Functions
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation