------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
1:strlen:计算字符串的长度
函数名:strlen
原型:int strlen(char *s);
用法:#include <string.h>
功能:计算字符串s的长度
说明:返回s的长度,不包括结束符NULL。
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf[SIZE] = "";
int len = 0;
gets(buf);
len = strlen(buf);//计算字符串的长度
printf("len = %d\n", len);
return 0;
}
2:atoi:字符串转整型
buf = “1379”;”-387”
atoi(buf)—>1379 , -387
原型:int atoi(const char *nptr);
参数说明:参数nptr字符串
头文件: #include <stdlib.h>
功能:把字符串转换成整型数。ASCII to integer 的缩写。
说明:如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0)字符时停止转换,返回整型数。否则,返回零
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf[SIZE] = "";
gets(buf);
printf("atoi(buf) = %d\n", atoi(buf));
return 0;
}
3:strcpy:字符串的复制
原型声明:char *strcpy(char* dest, const char *src);
头文件:#include <string.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf1[SIZE] = "";
char buf2[SIZE] = "";
gets(buf2);
strcpy(buf1, buf2);
printf("buf1 = %s\n", buf1);
printf("buf2 = %s\n", buf2);
return 0;
}
4:strcat:字符串的拼接
buf1 = bei jing
buf2 = huan ying nin
strcat(buf1, buf2);
函数名:strcat
原型: char *strcat(char *dest,char *src);
参数说明:参数nptr字符串
头文件: #include <string.h>
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf1[SIZE] = "";
char buf2[SIZE] = "";
gets(buf1);
gets(buf2);
strcat(buf1, buf2);
printf("buf1 = %s\n", buf1);
printf("buf2 = %s\n", buf2);
return 0;
}
5:strcmp:字符串的比较
函数名:strcmp
原型:int strcmp(const char *s1,const char * s2);
所在头文件:#include <string.h>
功能:比较字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)
说明:
当s1<s2时,返回为负数注意不是-1
当s1==s2时,返回值= 0
当s1>s2时,返回正数注意不是1
即:两个字符串自左向右逐个字符相比
(按ASCII值大小相比较),直到出现不同
的字符或遇'\0'为止。
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf1[SIZE] = "";
char buf2[SIZE] = "";
int t = 0;
gets(buf1);
gets(buf2);
t = strcmp(buf1, buf2);
if (t < 0){
printf("buf1 < buf2\n");
} else if(t == 0) {
printf("buf1 = buf2\n");
} else{
printf("buf1 > buf2\n");
}
return 0;
}
6:strchr:字符串中查找某个字符
buf = “skhdhkf”—>&buf[2] ——>buf+2—>printf(buf+2)
ch = ‘h’
函数名:strchr
char *strchr(const char* _Str,int _Val)
char *strchr(char* _Str,int _Ch)
头文件:#include <string.h>
功能:查找字符串s中首次出现字符c的位置
说明:返回首次出现c的位置的指针,返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串位置,如果s中不存在c则返回NULL。
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf[SIZE] = "";
char ch = 0;
gets(buf);
ch = getchar();
printf("%s\n", strchr(buf, ch));
return 0;
}
7:strstr:字符串中查找子串
buf1 = bei jing ei sdjkfhk ——>buf+1—>printf——>
buf2 = ei
函数名: strstr
头文件:#include <string.h>
函数原型:char *strstr(const char *str1, const char *str2);
语法:* strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:该函数返回str2第一次在str1中的位置,如果没有找到,返回NULL
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, const char *argv[])
{
char buf1[SIZE] = "";
char buf2[SIZE] = "";
gets(buf1);
gets(buf2);
printf("%s\n", strstr(buf1, buf2));
return 0;
}