Previous Section 2.13 stdlib.h |
| Table of Contents | Index | |
Next Section
2.15 time.h |
The string header provides many functions useful for manipulating strings (character arrays).
Macros:
NULL
Variables:
typedef size_t
Functions:
memchr();
memcmp();
memcpy();
memmove();
memset();
strcat();
strncat();
strchr();
strcmp();
strncmp();
strcoll();
strcpy();
strncpy();
strcspn();
strerror();
strlen();
strpbrk();
strrchr();
strspn();
strstr();
strtok();
strxfrm();
Declaration:
Returns a pointer pointing to the first matching character, or null if no match was found.
Declaration:
Returns zero if the first n bytes of str1 and str2 are equal. Returns less than zero or
greater than zero if str1 is less than or greater than str2 respectively.
Declaration:
Declaration:
Declaration:
Declaration:
The argument str1 is returned.
Declaration:
The argument str1 is returned.
Declaration:
Returns a pointer pointing to the first matching character, or null if no match was found.
Declaration:
Returns zero if str1 and str2 are equal. Returns less than zero or greater than zero if str1
is less than or greater than str2 respectively.
Declaration:
Returns zero if the first n bytes (or null terminated length) of str1 and str2 are equal.
Returns less than zero or greater than zero if str1 is less than or greater than str2 respectively.
Declaration:
Returns zero if str1 and str2 are equal. Returns less than zero or greater than zero if str1
is less than or greater than str2 respectively.
Declaration:
Declaration:
Declaration:
Returns the length of this first sequence of characters found that do not match with str2.
Declaration:
Returns a pointer to an error message string.
Declaration:
Returns the number of characters in the string.
Declaration:
A pointer to the location of this character is returned. A null pointer is returned if no
character in str2 exists in str1.
Example:
Declaration:
Returns a pointer pointing to the last matching character, or null if no match was found.
Declaration:
Returns the length of this first sequence of characters found that match with str2.
Example:
Declaration:
Returns a pointer to the first occurrence of str2 in str1. If no match was found, then a
null pointer is returned. If str2 points to a string of zero length, then the argument str1 is
returned.
Declaration:
Subsequent calls with a null pointer for str1 will cause the previous position saved to be
restored and begins searching from that point. Subsequent calls may use a different value for
str2 each time.
Returns a pointer to the first token in str1. If no token is found then a null pointer is
returned.
Example:
Declaration:
Returns the length of the transformed string (not including the null character).
NULL
is the value of a null pointer constant.
2.14.2 memchr
Searches for the first occurrence of the character c (an
void *memchr(const void *
str, int
c, size_t
n);
unsigned char
) in the first n bytes
of the string pointed to by the argument str.
2.14.3 memcmp
Compares the first n bytes of str1 and str2. Does not stop comparing even after the null
character (it always checks n characters).
int memcmp(const void *
str1, const void *
str2, size_t
n);
2.14.4 memcpy
Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is undefined.
void *memcpy(void *
str1, const void *
str2, size_t
n);
2.14.5 memmove
Copies n characters from str2 to str1. If str1 and str2 overlap the information is first
completely read from str1 and then written to str2 so that the characters are copied correctly.
void *memmove(void *
str1, const void *
str2, size_t
n);
2.14.6 memset
Copies the character c (an
void *memset(void *
str, int
c, size_t
n);
unsigned char
) to the first n characters of the string pointed to
by the argument str.
2.14.7 strcat
Appends the string pointed to by str2 to the end of the string pointed to by str1. The
terminating null character of str1 is overwritten. Copying stops once the terminating null
character of str2 is copied. If overlapping occurs, the result is undefined.
char *strcat(char *
str1, const char *
str2);
2.14.8 strncat
Appends the string pointed to by str2 to the end of the string pointed to by str1 up to n
characters long. The terminating null character of str1 is overwritten. Copying stops once n
characters are copied or the terminating null character of str2 is copied. A terminating null
character is always appended to str1. If overlapping occurs, the result is undefined.
char *strncat(char *
str1, const char *
str2, size_t
n);
2.14.9 strchr
Searches for the first occurrence of the character c (an unsigned char) in the string
pointed to by the argument str. The terminating null character is considered to be part of the
string.
char *strchr(const char *
str, int
c);
2.14.10 strcmp
Compares the string pointed to by str1 to the string pointed to by str2.
int strcmp(const char *
str1, const char *
str2);
2.14.11 strncmp
Compares at most the first n bytes of str1 and str2. Stops comparing after the null
character.
int strncmp(const char *
str1, const char *
str2, size_t
n);
2.14.12 strcoll
Compares string str1 to str2. The result is dependent on the
int strcoll(const char *
str1, const char *
str2);
LC_COLLATE
setting of the
location.
2.14.13 strcpy
Copies the string pointed to by str2 to str1. Copies up to and including the null character
of str2. If str1 and str2 overlap the behavior is undefined.
char *strcpy(char *
str1, const char *
str2);
2.14.14 strncpy
Copies up to n characters from the string pointed to by str2 to str1. Copying stops when
n characters are copied or the terminating null character in str2 is reached. If the null character
is reached, the null characters are continually copied to str1 until n characters have been copied.
char *strncpy(char *
str1, const char *
str2, size_t
n);
2.14.15 strcspn
Finds the first sequence of characters in the string str1 that does not contain any character
specified in str2.
size_t strcspn(const char *
str1, const char *
str2);
2.14.16 strerror
Searches an internal array for the error number errnum and returns a pointer to an error
message string.
char *strerror(int
errnum);
2.14.17 strlen
Computes the length of the string str up to but not including the terminating null
character.
size_t strlen(const char *
str);
2.14.18 strpbrk
Finds the first character in the string str1 that matches any character specified in str2.
char *strpbrk(const char *
str1, const char *
str2);
The output should result in every space in the string being converted to a dash (-).
#include<string.h>
#include<stdio.h>
int main(void)
{
char string[]="Hi there, Chip!";
char *string_ptr;
while((string_ptr=strpbrk(string," "))!=NULL)
*string_ptr='-';
printf("New string is \"%s\".\n",string);
return 0;
}
2.14.19 strrchr
Searches for the last occurrence of the character c (an
char *strrchr(const char *
str, int
c);
unsigned char
) in the string
pointed to by the argument str. The terminating null character is considered to be part of the
string.
2.14.20 strspn
Finds the first sequence of characters in the string str1 that contains any character
specified in str2.
size_t strspn(const char *
str1, const char *
str2);
The output should be:
The number length is 4.
#include<string.h>
#include<stdio.h>
int main(void)
{
char string[]="7803 Elm St.";
printf("The number length is %d.\n",strspn(string,"1234567890"));
return 0;
}
2.14.21 strstr
Finds the first occurrence of the entire string str2 (not including the terminating null
character) which appears in the string str1.
char *strstr(const char *
str1, const char *
str2);
2.14.22 strtok
Breaks string str1 into a series of tokens. If str1 and str2 are not null, then the following
search sequence begins. The first character in str1 that does not occur in str2 is found. If str1
consists entirely of characters specified in str2, then no tokens exist and a null pointer is
returned. If this character is found, then this marks the beginning of the first token. It then
begins searching for the next character after that which is contained in str2. If this character is
not found, then the current token extends to the end of str1. If the character is found, then it is
overwritten by a null character, which terminates the current token. The function then saves the
following position internally and returns.
char *strtok(char *
str1, const char *
str2);
This program replaces each space into a null character and stores a pointer to each substring into
the array. It then prints out each item.
#include<string.h>
#include<stdio.h>
int main(void)
{
char search_string[]="Woody Norm Cliff";
char *array[50];
int loop;
array[0]=strtok(search_string," ");
if(array[0]==NULL)
{
printf("No test to search.\n");
exit(0);
}
for(loop=1;loop<50;loop++)
{
array[loop]=strtok(NULL," ");
if(array[loop]==NULL)
break;
}
for(loop=0;loop<50;loop++)
{
if(array[loop]==NULL)
break;
printf("Item #%d is %s.\n",loop,array[loop]);
}
return 0;
}
2.14.23 strxfrm
Transforms the string str2 and places the result into str1. It copies at most n characters
into str1 including the null terminating character. The transformation occurs such that
size_t strxfrm(char *
str1, const char *
str2, size_t
n);
strcmp
applied to two separate converted strings returns the same value as strcoll
applied to the same
two strings. If overlapping occurs, the result is undefined.
Previous Section
2.13 stdlib.h
| Table of Contents |
Index |
Next Section
2.15 time.h