Your browser doesn't support JavaScript String Manipulation - Windows Programming

String Manipulation

To address the task of string manipulation, C supports large numbers of string handling functions in the standard library “string.h”. The more useful string-handling functions are discussed below:

strlen

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 <stdio.h>
#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;
}

strcpy()

The library function strcpy() copies an entire character array to another character array. Its prototype is as follows:

char* strcpy(char* destination, const char* source);

The following short program demonstrates the strcpy function

#include <stdio.h>
#include <string.h>
int main()
{
 char str1[10]= "copy array";
 char str2[10];
 strcpy(str2, str1);
 puts(str2);
 return 0;
}

strncpy()

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);

Copies up to n characters from the string pointed in source to the string pointer in 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 <stdio.h>
#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;
}

strcat()

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 in this new array to hold the copied string. The prototype of strcat() is

char *strcat(char *str1, const char *str2);

The following short program demonstrates the strcat function

#include <stdio.h>
#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;
}

strncat()

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

/* 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;
}

strcmp()

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. The prototype of strcmp() is

int strcmp(const char *str1, const char *str2);

For return values see the table below:

return valueindicates
<0the first character that does not match has a lower value in ptr1 than in ptr2
0the contents of both strings are equal
>0the first character that does not match has a greater value in ptr1 than in ptr2

The following short program demonstrates the strcmp function

#include <stdio.h>
#include <string.h>
char str1[] = "123456789";
char str2[] = "12346789";
int main( void )
{
int i=strcmp(str1,str2);
printf("\n%i ",i);
return 0;
}

strncmp

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

/* The strcmp() function. */
#include <stdio.h>
#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;
}

strchr

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 search direction 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

/* Thestrchr() function. */
#include <stdio.h>
#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;
}

strrchr()

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. 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 no match is found. 

strstr()

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 it 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 <stdio.h>
#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;
}