To address the task of strings manipulation, C supports a large number of string handling functions in the standard library "string.h". A of more useful string handling functions are discussed below:
The strlen (str) function takes a single argument, in the form of a character and returns the length of the string. The strlen() function is defined in <string.h> header file. The prototype of strlen is
size_t strlen(const char *str);
The following short program demonstrates the strlen function
#include <string.h>
int main( void )
{
size_t length;
char buf[100];
puts("\nEnter a line of text, a blank line to exit.");
gets(buf);
length = strlen(buf);
printf("\nThat line is %u characters long.", length);
return 0;
}
The library function strcpy() copies an entire character array to another character array. Its
prototype is as follows:
#include <string.h>
int main()
{
char str1[10]= "copy array";
char str2[10];
strcpy(str2, str1);
puts(str2);
return 0;
}
The library function strncpy() copies a specified number of characters from one character array to another character array. Its prototype is
char *strncpy(char *destination, const char *source, size_t n);
Copies up to n characters from the string pointed to, by the source to the string pointer to by destination. In a case where the length of src is less than that of n, the remainder of dest will be padded with null bytes.
The following short program demonstrates the strncpy function
#include <string.h>
char dest_string[] = "";
char source_string[] = "copied";
int main( void )
{
size_t n=6;
printf("\nBefore strncpy destination = %s", dest_string);
strncpy(dest_string, source_string, n);
printf("\nAfter strncpy destination = %s\n",source_string);
return 0;
}
The function appends a copy of a char array (str2) onto the end of another char array (str2), moving the terminating null character to the end of the new array. There must be enough space allocated in this new array to hold the copied string. The prototype of strcat() is
#include <string.h>
char source[20] = "source";
char destination[20] = "destination ";
int main( void )
{
printf("This is the source string = %s\n", source);
printf("This is the destination string = %s\n", destination);
strcat(destination,source);
printf("Combined string = %s\n", destination);
return 0;
}
The library function strncat() also performs string concatenation but has an additional option to specify how many characters of the source string are appended to the end of the destination string. The prototype is
char *strncat(char *str1, const char *str2, size_t n);
The following short program demonstrates the strncat function
#include <stdio.h>
#include <string.h>
char str1[] = "123456789";
int main( void )
{
char str2[27];
strcpy(str2,"");
strncat(str2, str1, 5);
puts(str2);
return 0;
}
The function strcmp() compares two strings, character by character. This function starts by comparing the first character of each string. If they are equal then it continues with the following pairs until the characters differ or until a terminating null-character is reached.
Its prototype is
int strcmp(const char *str1, const char *str2);
For return values see table below:
return value | indicates |
---|---|
<0 | the first character that does not match has a lower value in ptr1 than in ptr2 |
0 | the contents of both strings are equal |
>0 | the first character that does not match has a greater value in ptr1 than in ptr2 |
The following short program demonstrates the strcmp function
#include <string.h>
char str1[] = "123456789";
char str2[] = "12346789";
int main( void )
{
int i=strcmp(str1,str2);
printf("\n%i ",i);
return 0;
}
The library function strncmp() compares a specified number of characters of one char array to another char array. The method of comparison and return values are the same as for strcmp(). The prototype is
int strncmp(const char *str1, const char *str2, size_t n);
The following short program demonstrates the strncmp function
#include <string.h>
char str1[] = "123456789";
char str2[] = "12346789";
int main( void )
{
int i=strncmp(str1,str2,7);
printf("\n%i ",i);
//puts(i);
return 0;
}
The strchr() function searches for the first occurrence of a specified character in a string. The prototype is
char *strchr(constchar *str, int ch);
The direction of search is from left to right until the character ch is found or the terminating null character is found. The returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found. Since str is a pointer to the first character in the string, the position of the found character in the array can be calculated by subtracting str from the pointer value returned by strchr()
The following short program demonstrates the strchr function
#include <string.h>
char *location;
char str1[] = "123456789";
char ch='7';
int position=0;
int main( void )
{
location=strchr (str1, ch);
position=location-str1;
printf("\n%i ",position);
return 0;
}
The library function strrchr() is identical to strchr() , with the exception that it searches a char array for the last occurrence of a specified character in a string. The prototype is
char *strrchr(const char *str, int ch);
The function strrchr() returns a pointer to the last occurrence of ch in str and NULL if it finds no match.
This function performs a case-sensitive search for the first occurrence of one string within another string. Its prototype is
char *strstr(const char *str1, const char *str2);
The function strstr() returns a pointer to the first occurrence of str2 within str1 . If the search does not find a match, then the function returns NULL. When strstr() finds a match, the position of the substring can be calculated by subtracting str from the pointer value returned by strchr().
The following short program demonstrates the strstr function
#include <string.h>
char str1[] = "123456789";
char str2[] = "56";
char *position;
int main( void )
{
char *position;
position=strstr(str1, str2);
printf("\n%i ",position-str1);
return 0;
}
Last Updates:25 October 2022