0% found this document useful (0 votes)
21 views

11 - String Handling Library

The document describes the string handling library string.h and some commonly used string handling functions within it. The string handling functions allow copying, appending, comparing, and finding the length of strings. Functions like strcpy, strncpy, strcat, strncat copy and append strings. Functions like strcmp, strncmp compare strings, and strlen returns the length of a string. An example program is provided that finds and replaces all instances of a pattern string to uppercase within a larger text string, and outputs the number of replacements.

Uploaded by

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

11 - String Handling Library

The document describes the string handling library string.h and some commonly used string handling functions within it. The string handling functions allow copying, appending, comparing, and finding the length of strings. Functions like strcpy, strncpy, strcat, strncat copy and append strings. Functions like strcmp, strncmp compare strings, and strlen returns the length of a string. An example program is provided that finds and replaces all instances of a pattern string to uppercase within a larger text string, and outputs the number of replacements.

Uploaded by

kneel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STRING HANDLING LIBRARY (string.

h)

The following are the commonly used string handling functions. To use these functions, include string.h
library in your program.

Function Prototype Function Description


char *strcpy (char *s1, const char *s2) Copies string s2 into array s1. The value of s1 is
returned.
char *strncpy (char *s1, const char *s2, size_t n) Copies at most n characters of string s2 into array
s1. The value of s1 is returned.
char *strcat (char *s1, const char *s2) Appends string s2 to array s1. The first character
of s2 overwrites the terminating null character of
s1. The value of s1 is returned.
char *strncat (char *s1, const char *s2, size_t n) Appends at most n characters of string s2 to array
s1. The first character of s2 overwrites the
terminating null character of s1. The value of s1 is
returned.
int strcmp (const char *s1, const char *s2) Compares the string s1 with the string s2. The
function returns 0, less than or greater than 0 if s1
is equal to, less than or greater than s2,
respectively.
int strncmp (const char *s1, const char *s2, Compares up to n characters of the string s1 with
size_t n) the string s2. The function returns 0, less than 0 or
greater than 0 is s1 is equal to, less than or greater
than s2, respectively.
long int strlen(const char *s1) Returns the length of the string

Function Example Output

strcpy

strncpy
strcat

strncat

strcmp

strncmp

strlen
Exercise

Write a program that would match a string. It accepts two inputs: the phrase/sentence string (text) and
the pattern string (word). The program finds the first (or all) instances of the pattern in the text and
changes that word in all uppercase and displays its number of occurrences.

Sample Input:

Text string: You will always have my love, my love, for the love I love is as lovely as love itself.

Pattern string: love

Sample Output:

New text: You will always have my LOVE, my LOVE, for the LOVE I LOVE is as lovely as LOVE itself.

Number of occurrence: 5

Filename: stringmatch.c

You might also like