C语言---字符串处理函数
2016-08-25 17:02
141 查看
1. strcat
char *strcat(char *s, char *t) 把字符串t连接到s后面,返回值为s
2. strcmp
int strcmp(char *s, char *t) 逐个比较字符串 s 和 t 中对应的字符,直到对应字符不等或比较到串尾. 如果相等返回0,不等返回1;
3. strcpy
char strcpy(char *s, char *t) 把t复制到字符串s中,s原来的字符串内容消失。返回值为现在的s值,即t的内容。
4. strlen
unsigned int strlen(char *s) 计算字符串中 s 的长度(不包括’\0’),即有效长度,返回值为s的长度整数。
5. strchr
char strchr(char *s, char t) 在s字符串中找到t字符首次出现的位置,返回值是t的地址,输出显示是t及以的字符串。
6. strstr
char *strstr(char *s, char *t) 在字符串 s 中查找字符串 t 首次出现的地址.找到返回值为相应地址,未找到返回值为NULL。
char *strcat(char *s, char *t) 把字符串t连接到s后面,返回值为s
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t[10]; gets(t); strcat(s,t); printf("%s\n",s); printf("%s\n",t); return 0; }
2. strcmp
int strcmp(char *s, char *t) 逐个比较字符串 s 和 t 中对应的字符,直到对应字符不等或比较到串尾. 如果相等返回0,不等返回1;
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t[10]; gets(t); int n; n = strcmp(s,t); printf("%d\n",n); printf("%s\n",s); printf("%s\n",t); return 0; }
3. strcpy
char strcpy(char *s, char *t) 把t复制到字符串s中,s原来的字符串内容消失。返回值为现在的s值,即t的内容。
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t[10]; gets(t); char *n; n = strcpy(s,t); printf("n = %s\n",n); printf("s = %s\n",s); printf("t = %s\n",t); return 0; }
4. strlen
unsigned int strlen(char *s) 计算字符串中 s 的长度(不包括’\0’),即有效长度,返回值为s的长度整数。
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t[10]; gets(t); printf("s = %s len = %d\n",s,strlen(s)); printf("t = %s len = %d\n",t,strlen(t)); return 0; }
5. strchr
char strchr(char *s, char t) 在s字符串中找到t字符首次出现的位置,返回值是t的地址,输出显示是t及以的字符串。
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t; t = getchar(); printf("数值形式:*p = %d \n",*strchr(s,t)); printf("字符形式:*p = %s \n",strchr(s,t)); return 0; }
6. strstr
char *strstr(char *s, char *t) 在字符串 s 中查找字符串 t 首次出现的地址.找到返回值为相应地址,未找到返回值为NULL。
#include <stdio.h> #include <string.h> int main(void){ char s[10]; gets(s); char t[10]; gets(t); printf("数值形式:*p = %d \n",*strstr(s,t)); printf("字符形式:*p = %s \n",strstr(s,t)); return 0; }
相关文章推荐