1. strcpy
char * strcpy ( char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
Return Value
destination is returned.
char* Strcpy(char* destination, const char* source) {
assert(destination && source);
char* dest = destination;
const char* src = source;
while (*src) {
*dest++ = *src++;
}
*dest = *src;
return destination;
}
2. strcat
char * strcat ( char * destination, const char * source );
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
destination and source shall not overlap.
Return Value
destination is returned.
char* Strcat(char* destination, const char* source) {
assert(destination && source);
char* dest = destination;
const char* src = source;
while (dest) {
dest++;
}
while (src) {
*dest++ = *src++;
}
*dest = *src;
return destination;
}
3. strstr
char * strstr ( const char *, const char * );
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters, but it stops there.
char* Strstr(const char* str1, const char* str2) {
assert(str1 && str2);
const char* ptr1 = NULL;
const char* ptr2 = NULL;
while (str1) {
if (*str1 == *str2) {
ptr1 = str1;
ptr2 = str2;
while (*ptr2) {
if (*ptr1 != *ptr2) {
break;
}
ptr1++;
ptr2++;
}
if (*ptr2 == '\0') {
return str1;
}
}
str1++;
}
return NULL;
}
4. strchr
char * strchr ( const char *, int );
Returns a pointer to the first occurrence of character in the C string str.
The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.
char* Strchr(const char* str, int character) {
assert(str);
const char* ptr = str;
while (ptr) {
if (*ptr == character) {
return ptr;
}
ptr++;
}
return NULL;
}
5. strcmp
int strcmp ( const char * str1, const char * str2 );
Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
Return Value
- less than 0 : the first character that does not match has a lower value in ptr1 than in ptr2
- equals 0 : the contents of both strings are equal
- large than 0 : the first character that does not match has a greater value in ptr1 than in ptr2
int Strcmp(const char* str1, const char* str2) {
assert(str1 && str2);
while (1) {
if (*str1 > *str2) {
return 1;
}
else if (*str1 < *str2) {
return -1;
}
else if (*str1 == '\0' && *str2 == '\0') {
return 0;
}
str1++;
str2++;
}
}
6. memcpy
void * memcpy ( void * destination, const void * source, size_t num );
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.
The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed to by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).
Parameters
destination : Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
source : Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
num : Number of bytes to copy. size_t is an unsigned integral type.
void* Memcpy(void* destination, const void* source, size_t num) {
assert(destination && source);
char* dest = (char *)destination;
const char* src = (const char *)source;
while (num-- > 0) {
*dest++ = *src++;
}
return destination;
}
7. memmove
void * memmove ( void * destination, const void * source, size_t num );
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least numbytes.
void* Memmove(void* destination, const void* source, size_t num) {
assert(destination && source);
char* dest = (char *)destination;
const char* src = (const char *)source;
while (num > 0) {
if (destination > source) {
*(dest + num - 1) = *(src + num - 1);
}
else {
*(dest++) = *(src++);
}
num--;
}
return destination;
}