strlen()
The "strlen()" function gives the length of a string, not including the NULL character at the end:/* strlen.c */ #include <stdio.h> #include <string.h> void main() { char *t = "XXX"; printf( "Length of <%s> is %d.\n", t, strlen( t )); }
Length of <XXX> is 3.
strcpy()
The "strcpy" function copies one string from another. For example:/* strcpy.c */ #include <stdio.h> #include <string.h> void main() { char s1[100], s2[100]; strcpy( s1, "string 1" ); strcpy( s2, "string 2" ); puts( "Original strings: " ); puts( "" ); puts( s1 ); puts( s2 ); puts( "" ); strcpy( s2, s1 ); puts( "New strings: " ); puts( "" ); puts( s1 ); puts( s2 ); }
Original strings: string 1 string 2 New strings: string 1 string 1Please be aware of two features of this program:
- This program assumes that "s1" has enough space to store the final string. The "strcpy()" function won't bother to check, and will give erroneous results if that is not the case.
- A string constant can be used as the source string instead of a string variable. Using a string constant for the destination, of course, makes no sense.
strncpy()
There is a variant form of "strcpy" named "strncpy" that will copy "n" characters of the source string to the destination string, presuming there are that many characters available in the source string. For example, if the following change is made in the example program:strncpy( s2, s1, 5 );
New strings: string 1 stringNotice that the parameter "n" is declared "size_t", which is defined in "string.h".
strcat()
The "strcat()" function joins two strings:/* strcat.c */ #include <stdio.h> #include <string.h> void main() { char s1[50], s2[50]; strcpy( s1, "Tweedledee " ); strcpy( s2, "Tweedledum" ); strcat( s1, s2 ); puts( s1 ); }
Tweedledee Tweedledum
strncat()
There is a variant version of "strcat()" named "strncat()" that will append "n" characters of the source string to the destination string. If the example above used "strncat()" with a length of 7:strncat( s1, s2, 7 );
Tweedledee TweedleAgain, the length parameter is of type "size_t".
strcmp()
The "strcmp()" function compares two strings:/* strcmp.c */ #include <stdio.h> #include <string.h> #define ANSWER "blue" void main() { char t[100]; puts( "What is the secret color?" ); gets( t ); while ( strcmp( t, ANSWER ) != 0 ) { puts( "Wrong, try again." ); gets( t ); } puts( "Right!" ); }
There are three alternate forms for "strcmp()":
strncmp()
- A "strncmp()" function which, as might be guessed, compares "n" characters in the source string with the destination string:
"strncmp( s1, s2, 6 )".
stricmp()
- A "stricmp()" function that ignores case in comparisons.
strnicmp()
- A case-insensitive version of "strncmp" called "strnicmp".
strchr()
The "strchr" function finds the first occurrence of a character in a string. It returns a pointer to the character if it finds it, and null if not. For example:/* strchr.c */ #include <stdio.h> #include <string.h> void main() { char *t = "MEAS:VOLT:DC?"; char *p; p = t; puts( p ); while(( p = strchr( p, ':' )) != NULL ) { puts( ++p ); } }
MEAS:VOLT:DC? VOLT:DC? DC?The character is defined as a character constant, which C regards as an "int". Notice how the example program increments the pointer before using it ("++p") so that it doesn't point to the ":" but to the character following it.
strrchr()
The "strrchr()" function is almost the same as "strchr()", except that it searches for the last occurrence of the character in the string.strstr()
The "strstr()" function is similar to "strchr()" except that it searches for a string, instead of a character. It also returns a pointer:char *s = "Black White Brown Blue Green"; ... puts( strstr( s, "Blue" ) );
strlwr() and strupr()
The "strlwr()" and "strupr()" functions simply perform lowercase or uppercase conversion on the source string. For example:/* casecvt.c */ #include <stdio.h> #include <string.h> void main() { char *t = "Hey Barney hey!"; puts( strlwr( t ) ); puts( strupr( t ) ); }
hey barney hey! HEY BARNEY HEY!These two functions are only implemented in some compilers and are not part of ANSI C.