StartString FunctionsI think everyone is familiar with it. String functions perform different operations on binary data, strings, and expressions. The following describes the string functions in C.
13. Function Name: strncmpi
Function: compares a part of a string with a part of another string, regardless of case.
Usage:
- int strncmpi(char *str1, char *str2);
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char *buf1 = "BBBccc", *buf2 = "bbbccc";
- int ptr;
- ptr = strncmpi(buf2,buf1,3);
- if (ptr > 0)
- printf("buffer 2 is greater than buffer 1\n");
- if (ptr < 0)
- printf("buffer 2 is less than buffer 1\n");
- if (ptr == 0)
- printf("buffer 2 equals buffer 1\n");
- return 0;
- }
14. Function Name: strncpy
Function: Copy strings
Usage:
- char *strncpy(char *destin, char *source, int maxlen);
Program example:
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char string[10];
- char *str1 = "abcdefghi";
- strncpy(string, str1, 3);
- string[3] = '\0';
- printf("%s\n", string);
- return 0;
- }
15. Function Name: strnicmp
Function: Compares two strings case-insensitive.
Usage:
- int strnicmp(char *str1, char *str2, unsigned maxlen);
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char *buf1 = "BBBccc", *buf2 = "bbbccc";
- int ptr;
- ptr = strnicmp(buf2, buf1, 3);
- if (ptr > 0)
- printf("buffer 2 is greater than buffer 1\n");
- if (ptr < 0)
- printf("buffer 2 is less than buffer 1\n");
- if (ptr == 0)
- printf("buffer 2 equals buffer 1\n");
- return 0;
- }
16. Function Name: strnset
Function: Set all characters in a string to a specified character.
Usage:
- char *strnset(char *str, char ch, unsigned n);
Program example:
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char *string = "abcdefghijklmnopqrstuvwxyz";
- char letter = 'x';
- printf("string before strnset: %s\n", string);
- strnset(string, letter, 13);
- printf("string after strnset: %s\n", string);
- return 0;
- }
17. Function Name: strpbrk
Function: searches for characters in a given character set in a string.
Usage:
- char *strpbrk(char *str1, char *str2);
Program example:
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char *string1 = "abcdefghijklmnopqrstuvwxyz";
- char *string2 = "onm";
- char *ptr;
- ptr = strpbrk(string1, string2);
- if (ptr)
- printf("strpbrk found first character: %c\n", *ptr);
- else
- printf("strpbrk didn't find character in set\n");
- return 0;
- }
18. Function Name: strrchr
Function: Find the last occurrence of a specified character in the string.
Usage:
- char *strrchr(char *str, char c);
Program example:
- #include <string.h>
- #include <stdio.h>
-
- int main(void)
- {
- char string[15];
- char *ptr, c = 'r';
-
- strcpy(string, "This is a string");
- ptr = strrchr(string, c);
- if (ptr)
- printf("The character %c is at position: %d\n", c, ptr-string);
- else
- printf("The character was not found\n");
- return 0;
- }
19. Function Name: strrev
Power: String reversal
Usage:
Char * strrev (char * str );
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char *forward = "string";
- printf("Before strrev(): %s\n", forward);
- strrev(forward);
- printf("After strrev(): %s\n", forward);
- return 0;
- }