2. String.h- This header file defines several functions to manipulate C strings and arrays. Functions declared in string.h are extremely popular since as a part of the C standard library, they are guaranteed to work on any platform which supports C Some of its functions are defined below .
3. Memcpy:- Use Copy block of memory SYNOPSIS #include <string.h> void *memcpy(void *s1, const void *s2, size_tn); DESCRIPTION The memcpy() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behaviour is undefined. RETURN VALUE The memcpy() function returns s1; no return value is reserved to indicate an error.
5. memmove memmove Memmove:- Use Move block of memory SYNOPSIS #include <string.h> void *memmove(void *s1, const void *s2, size_tn); DESCRIPTION The memmove() function copies n bytes from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n bytes from the object pointed to by s2 are first copied into a temporary array of n bytes that does not overlap the objects pointed to by s1 and s2, and then the n bytes from the temporary array are copied into the object pointed to by s1. RETURN VALUE The memmove() function returns s1; no return value is reserved to indicate an error.
6. Example /* memmove example */ #include <stdio.h> #include <string.h> int main () { char str[] = "memmove can be veryuseful......"; memmove (str+20,str+15,11); puts (str); return 0; }} } Output memmove can be very very useful.
7. Memcmp :- Use Compare two blocks of memory SYNOPSIS #include <string.h> intmemcmp(const void *s1, const void *s2, size_tn); DESCRIPTION The memcmp() function compares the first n bytes of the object pointed to by s1 to the first nbytes of the object pointed to by s2. The sign of a non-zero return value is determined by the sign of the difference between the values of the first pair of bytes that differ in the objects being compared. RETURN VALUE The memcmp() function returns an integer greater than, equal to or less than 0, if the object pointed to by s1 is greater than, equal to or less than the object pointed to by s2 respectively.
8. Example /* memcmp example */ #include <stdio.h> #include <string.h> int main () { char str1[256]; char str2[256]; int n; size_t len1, len2; printf ("Enter a sentence: "); gets(str1); printf ("Enter another sentence: "); gets(str2); len1=strlen(str1); len2=strlen(str2); n=memcmp ( str1, str2, len1>len2?len1:len2 ); if (n>0) printf ("'%s' is greater than '%s'.",str1,str2); else if (n<0) printf ("'%s' is less than '%s'.",str1,str2); else printf ("'%s' is the same as '%s'.",str1,str2); return 0; } Output Enter a sentence: building Enter another sentence: book 'building' is greater than 'book'
9. Memset:- Use Fill block of memory SYNOPSIS #include <string.h> void *memset(void *s, intc, size_tn); DESCRIPTION The memset() function copies c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s. RETURN VALUE The memset() function returns s; no return value is reserved to indicate an error.
10. Example /* memset example */ #include <stdio.h> #include <string.h> int main () { char str[] = "almost every programmer should know memset!"; memset (str,'-',6); puts (str); return 0; } Output ------ every programmer should know memset!
11. Memchr Use Locate character in block of memory SYNOPSIS #include <string.h> void *memchr(const void *s, intc, size_tn); DESCRIPTION The memchr() function locates the first occurrence of c (converted to an unsigned char) in the initial n bytes (each interpreted as unsigned char) of the object pointed to by s. RETURN VALUE The memchr() function returns a pointer to the located byte, or a null pointer if the byte does not occur in the object.
12. Example /* memchr example */ #include <stdio.h> #include <string.h> int main () { char * pch; char str[] = "Example string"; pch = (char*) memchr (str, 'p', strlen(str)); if (pch!=NULL) printf ("'p' found at position %d.", pch-str+1); else printf ("'p' not found."); return 0; } Output 'p' found at position 5.
13. Thanks for watching .Hope u like it….. By :- NISHANK