6.5. The string library¶
The include file string.h defines a variety of functions that can be used when working with strings.
6.5.1. String Manipulations¶
-
char *strcat( char *s1, char *s2)
Concatenate s1 and s2, result stored in s1.
-
char *strncat( char *s1, char *s2, n)
A safer version of strncat because it only concatenates the first n characters of s2.
-
strcmp
()¶
-
int strcmp(s1, s2)
compare two strings.
-
int strncmp(s1, s2, n)
compare first n characters of two strings.
-
char *strcpy(s1, s2)
copy string s2 into string s1.
-
char *strncpy(s1, s2, n)
copy first n characters of string s2 into string s1.
-
int strlen(char *s)
returns the length of the string.
-
char *strrchr( char *s, int ch )
find last char ch in string s.
-
char *strchr(char *s, int ch)
find first char ch in string s.
-
int strcspn(s1, s2)
find any char in s2 also in s1, returns index to the s1 string.
-
char *strpbrk(s1, s2)
Same as strcspn except it returns a pointer.
-
int strspn(s1, s2)
Opposite of strspn, it returns the index of the first char in s1 which is not in s2. Note, this is also the length of the initial segment of s1 which consists entirely of characters in s2.
-
char *strstr(s1, s2)
Search for a whole string s2 in s1.
-
double strtod(s, char *endp)
Convert a string containing numbers to a double. See Example Use of strtod and strtol.
-
long strtol(s, char *endp)
Convert a string containing numbers to a long. See Example Use of strtod and strtol.
-
int sprintf(char *str, const char *format, ...);
Just like printf() except the output goes to a string buffer, not the terminal.
char buff[50]; sprintf(buff, "%d %d\n", i, j);
-
char *strtok(s1,s2)
The string tokenizer. Returns a pointer to tokens in s1 which are deliminated by s2. The first call passes a pointer to s1, future calls to process the same string need to have NULL as the first argument.
token = strtok( s1, ":" ); /* get first token */ /* process first token */ while(((token = strtok(NULL,":")) != NULL ) { /* rest of tokens */ /* each token is processed */ }
Or, we could also combine the calls to strtok into a somewhat unusual for loop.
char *p, *q; for( q = s1; (p=strtok(q, ":")) != NULL; q = NULL ) { /* each token is processed */ }
6.5.2. Example Use of strtod and strtol¶
The following function can be used to apply strtod and strtol to applications where a string may contain a number. The second parameter passed to the function is a pointer to an int that is set by the function to indicate if the string is a number or other string data. This function always returns a double value. Integer strings are type cast to a double.
double convert_number( char *s, int *is_num )
{
char *endp;
double d;
long l;
d = strtod(s, &endp);
if (s != endp && *endp == '\0') {
*is_num = 1;
return d;
}
else
{
l = strtol(s, &endp, 0);
if (s != endp && *endp == '\0') {
*is_num = 1;
return (double)l;
}
else {
*is_num = 0; // not a number
return 0.0;
}
}
}
6.5.3. Useful Character Tests¶
Once in a while, when working string data, it is useful to test a character
value to see what type of character it is. The standard library includes
the following functions for this. They each take a single character
parameter and return a non-zero value if the test is true or zero if the test
is false. The function prototypes are in ctype.h
.
-
isalnum()
Test if character is either a letter from the alphabet or a numeric digit.
-
isalpha()
Test if character is a letter from the alphabet.
-
iscntrl()
Test if character is control character.
-
isdigit()
Test if character is a numeric digit 0 to 9
-
isgraph()
Test if character has a visible representation.
-
islower()
Test if character is a lower case letter from the alphabet.
-
isprint()
Test if character is printable.
-
ispunct()
Test if character is a punctuation mark.
-
isspace()
Test if character is the space or tab character.
-
isupper()
Test if character is an upper case letter from the alphabet.
-
isxdigit()
Test if character is hexadecimal digit.