StartString FunctionsI think everyone is familiar with it.String FunctionsPerform different operations on binary data, strings, and expressions. The following summaryC Language.
1. Function Name: stpcpy
Function: copy one string to another.
Usage:
- char *stpcpy(char *destin, char *source);
Program example:
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char string[10];
- char *str1 = "abcdefghi";
- stpcpy(string, str1);
- printf("%s\n", string);
- return 0;
- }
2. Function Name: strcat
Function: String concatenation Function
Usage:
- char *strcat(char *destin, char *source);
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char destination[25];
- char *blank = " ", *c = "C++", *Borland = "Borland";
- strcpy(destination, Borland);
- strcat(destination, blank);
- strcat(destination, c);
- printf("%s\n", destination);
- return 0;
- }
3. Function Name: strchr
Function: Find the first matching position of a given character in a string \
Usage:
- char *strchr(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 = strchr(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;
- }
4. Function Name: strcmp
Function: String comparison
Usage:
- int strcmp(char *str1, char *str2);
View Asic code, str1> str2, return value> 0; two strings are equal, return 0
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
- int ptr;
- ptr = strcmp(buf2, buf1);
- if (ptr > 0)
- printf("buffer 2 is greater than buffer 1\n");
- else
- printf("buffer 2 is less than buffer 1\n");
- ptr = strcmp(buf2, buf3);
- if (ptr > 0)
- printf("buffer 2 is greater than buffer 3\n");
- else
- printf("buffer 2 is less than buffer 3\n");
- return 0;
- }
5. Function Name: strncmpi
Function: compares a part of a string with another string regardless of case.
Usage:
- int strncmpi(char *str1, char *str2, unsigned maxlen);
Program example:
- #include <string.h>
- #include <stdio.h>
- int main(void)
- {
- char *buf1 = "BBB", *buf2 = "bbb";
- int ptr;
- ptr = strcmpi(buf2, buf1);
- 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;
- }
6. Function Name: strcpy
Function: Copy strings
Usage:
- char *strcpy(char *str1, char *str2);
Program example:
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char string[10];
- char *str1 = "abcdefghi";
- strcpy(string, str1);
- printf("%s\n", string);
- return 0;
- }