目录
1.strlwr strupr
char *strlwr(char *s1) 大写转小写
char *strupr(char *s1) 小写转大写
以下是对strlwr和struper的函数封装
char *strupr_s(char *src)//strupr封装
{
int i;
while(src[i]!='\0')
{
if(src[i]>='a'&&src[i]<='z')
{
src[i] -=32;
}
i++;
}
return src;
}
char *strlwr_s(char *src)//strlwr封装
{
int i;
while(src[i]!='\0')
{
if(src[i]>='A'&&src[i]<='Z')
{
src[i] +=32;
}
i++;
}
return src;
}
2.atoi atol atof
int atoi(const char *str)
把参数 str 所指向的字符串转换为一个整数(类型为 int 型)
long int atol(const char *str)
把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)
double atof(const char *str)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)
3.strtod
double strtod(const char *str, char **endptr)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[30] = "20.30300 This is test";
char *ptr;
double ret;
ret = strtod(str, &ptr);
printf("数字(double)是 %lf\n", ret);
printf("字符串部分是 |%s|", ptr);
return(0);
}
4.strtol,strtloul
将字符型的数,转换成strtol长整型和strtloul不带符号的长整型、
char str[20] ="82345671";
char *pch =NULL;
long ret4 =strtol(str,&pch,10);//有符号long
printf("%p\n",pch);
printf("%ld\n",ret4);
unsigned long ret5 =strtoul(str,&pch,10);//无符号long
printf("%p\n",pch);
printf("%lu\n",ret5);
5. memcpy, memove, memcmp
- memcpy可以拷贝除了字符类型的其他数据类型的数据,strcpy只能拷贝字符型数据;
他们两个的区别是strcpy只能拷贝字符类型的 ;memcpy()可以拷贝任意类型拷贝
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
// char src[20] ="hello world";
// char dest[30]="BSP2208class";
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int brr[20]={11,21,31,41,51,61,71,81,91,110,111,112,113};
// strcpy(dest,src);//只能拷贝字符串
// printf("%s\n",dest);
//
// memcpy(dest,src,10);//(目的数组)(拷贝数组)从src到dest
// printf("%s\n",dest);
memcpy(brr,arr,sizeof(int)*10);
int i=0;
for(i=0;i<20;i++)
{
printf("%d ",brr[i]);
}
return 0;
}
- memmove()和memcpy的区别就是memmove函数是处理的源内存块和目标内存块是可以重叠的。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *pa =NULL;
char *pb =NULL;
pa =malloc(sizeof(char)*20);
pb =realloc(pa,sizeof(char)*50);
printf("%p\n",pa);
printf("%p\n",pb);
strcpy(pa,"happy birthday to you");
strcpy(pb,"BSP2208 class");
memmove(pb,pa,sizeof(char)*10);
printf("%s\n",pa);
free(pb);
pb=NULL;
return 0;
}
- memcmp()把存储区str1和存储区str2的前n个字节进行比较。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//memcmp()把存储区str1和存储区str2的前n个字节进行比较
int main()
{
char *pa = NULL;
char *pb = NULL;
pa = malloc(sizeof(char)*10);
pb = malloc(sizeof(char)*20);
memset(pa,0,10);
memset(pb,0,10);
strcpy(pa,"BSP220 world");
strcpy(pb,"BSP22e8");
int ret = memcmp(pa,pb,6);
printf("%d\n",ret);
free(pa);
free(pb);
pa = NULL;
pb = NULL;
return 0;
}