string.h Flashcards
What does strcat() do?
Declaration:
char *strcat(char *str1, const char *str2);
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.
The argument str1 is returned.
What does memmove() do?
Declaration:
void *memmove(void *str1, const void *str2, size_t n);
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.
Returns the argument str1.
What does memcmp() do?
Declaration:
int memcmp(const void *str1, const void *str2, size_t n);
Compares the first n bytes of str1 and str2. Does not stop comparing even after the null character (it always checks n characters).
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.
What does memcpy() do?
Declaration:
void *memcpy(void *str1, const void *str2, size_t n);
Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is undefined.
Returns the argument str1.
What does strcpy() do?
Declaration:
char *strcpy(char *str1, const char *str2);
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.
Returns the argument str1.
What does strxfrm() do?
Declaration:
size_t strxfrm(char *str1, const char *str2, size_t n);
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 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.
Returns the length of the transformed string (not including the null character).
What does strlen() do?
Declaration:
size_t strlen(const char *str);
Computes the length of the string str up to but not including the terminating null character.
Returns the number of characters in the string.
What does strspn() do?
Declaration:
size_t strspn(const char *str1, const char *str2);
Finds the first sequence of characters in the string str1 that contains any character specified in str2.
Returns the length of this first sequence of characters found that match with str2.
Example:
#include #include
int main(void) { char string[]="7803 Elm St.";
printf(“The number length is %d.\n”,strspn(string,”1234567890”));
return 0;
}
The output should be: The number length is 4.
What does strrchr() do?
Declaration:
char *strrchr(const char *str, int c);
Searches for the last 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.
Returns a pointer pointing to the last matching character, or null if no match was found.
What does strerror() do?
Declaration:
char *strerror(int errnum);
Searches an internal array for the error number errnum and returns a pointer to an error message string.
Returns a pointer to an error message string.
What does strpbrk() do?
Declaration:
char *strpbrk(const char *str1, const char *str2);
Finds the first character in the string str1 that matches any character specified in str2.
A pointer to the location of this character is returned. A null pointer is returned if no character in str2 exists in str1.
Example:
#include #include
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;
}
The output should result in every space in the string being converted to a dash (-).
What does strstr() do?
Declaration:
char *strstr(const char *str1, const char *str2);
Finds the first occurrence of the entire string str2 (not including the terminating null character) which appears in the string str1.
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.
What does strtok() do?
Declaration:
char *strtok(char *str1, const char *str2);
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.
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:
#include #include
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
{
array[loop]=strtok(NULL,” “);
if(array[loop]==NULL)
break;
}
for(loop=0;loop
{
if(array[loop]==NULL)
break;
printf(“Item #%d is %s.\n”,loop,array[loop]);
}
return 0;
}
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.
What does memset() do?
Declaration:
void *memset(void *str, int c, size_t n);
Copies the character c (an unsigned char) to the first n characters of the string pointed to by the argument str.
The argument str is returned.
What does memchr() do?
Declaration:
void *memchr(const void *str, int c, size_t n);
Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to by the argument str.
Returns a pointer pointing to the first matching character, or null if no match was found.