1,字符串连接
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
英文manual并不难,直接上英文:
The strcat() function appends the src string to the dest string, over‐writing the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.The strings may not overlap, and the dest string must have enough space for the result.
The strncat() function is similar, except that
* it will use at most n characters from src; and
* src does not need to be null-terminated if it contains n or more characters.
As with strcat(), the resulting string in dest is always null-terminated. If src contains n or more characters, strncat() writes n+1 characters to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.
返回值:The strcat() and strncat() functions return a pointer to the resulting string dest.
示例代码:
-
#include <stdio.h>
-
#include <string.h>
-
-
int main(){
-
char *src="world";
-
char dest[100]="hello ";
-
strcat(dest,src);
-
printf("strcat result: %s, strlen: %d\n",dest,(int)strlen(dest));
-
return 0;
-
}
输出:
strcat result: hello world, strlen: 11
2,字符串查找
char *strstr(const char *haystack, const char *needle);
char *strcasestr(const char *haystack, const char *needle);
说明:The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared.
The strcasestr() function is like strstr(), but ignores the case of both arguments.
返回值:These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.
示例代码:
-
#include <stdio.h>
-
#include <string.h>
-
-
int main(){
-
char *src="hello world,I am a strstr example";
-
char *psubstr=strstr(src,"am");
-
printf("strstr result: %s\n",psubstr);
-
return 0;
-
}
输出:
strstr result: am a strstr example
阅读(401) | 评论(0) | 转发(0) |