linux c常用字符串处理函数

25 篇文章 1 订阅
一、puts
名称:
puts
功能: 
向显示器输出字符串。
头文件:
#include .h>
函数原形:
int puts(const char *s);
参数: 
s    字符串   
返回值: 
成功返回输出的字符数,失败返回EOF
put函数与printf函数在字符串输出中的区别:
puts在输出字符串时,遇到'\0'会自动终止输出,并将'\0'转换为'\n'来输出。
Printf在输出字符串时,遇到'\0'只是终止输出,并不会将'\0'转换为'\n'来输出。
二、strcat
名称:
strcat
功能: 
字符串连接函数
头文件:
#include .h>
函数原形:
char *strcat(char *restrict s1,const char *restrict s2);
参数: 
s1    字符串1
s2    字符串2
返回值: 
返回字符数组1的首地址
Strcat能够将字符串2连接到字符串1的后面。要注意的是字符串1必须能够装下字符串2。连接前,两串均以'\0'结束,连接后,串1的'\0'被取消,新串最后加‘'\0'
如:
char name[100]="Mike";
char number[20]="001";
strcat(name,number);
puts(name);
输出为:
Mike001
三、strcpy
名称:
strcpy
功能: 
字符串拷贝函数
头文件:
#include .h>
函数原形:
char *strcpy(char *restrict s1,const char *restrict s2);
参数: 
s1    字符串1
s2    字符串2
返回值: 
返回字符数组1的首地址
strcpy将字符串2,拷贝到字符数组1中去,要注意,字符数组1必须足够大,拷贝时'\0'一同拷贝,不能使用赋值语句为一个字符数组赋值。
四、strcmp
名称:
strcmp
功能: 
字符串比较函数
头文件:
#include .h>
函数原形:
char *strcmp(const char *s1,const char *s2);
参数: 
s1    字符串1
s2    字符串2
返回值: 
返回int型整数
strcmp对两串从左向右逐个字符比较(ASCLL吗),直到遇到不同字符或'\0'为止。若s1大于s2返回正整数,若s1小于s2返回负整数,若s1等于s2返回0。要注意字符串比较不能用"= =",必须用strcmp.
#include 
#include 
typedef struct
{
    char name[20];
    char num[20];
}USERINFO;
main()
{
    USERINFO user;
    char newname[ ]="Rose";
    int result;
    strcpy(user.name,"Mike");
    result=strcmp(user.name,newname);
    if(result!=0)
        printf("different person!");
    else
        Printf("the same person!");
} 
五、strlen
名称:
strlen
功能: 
字符串长度函数
头文件:
#include .h>
函数原形:
int strlen(const char *s);
参数: 
s    字符串
返回值: 
返回字符串实际长度
strlen计算字符串长度并返回,不包含'\0'在内。
如:char str[100]="study";
int length;
length=strlen(str);
printf("%d",length);
输出:5
六、strtok
名称:
strtok
功能: 
字符串分割函数
头文件:
#include .h>
函数原形:
char *strtok(char *s,const char *delim)
参数: 
s      欲分割的字符串
delim  分割字符串
返回值: 
返回下一个分割后的字符串指针,如果已无从分割则返回NULL
Strtok可将字符串分割,当strtok在参数s的字符串中发现到参?齞elim的分割字符时则会将该字符改为\0字符。
在第一次调用时,strtok必须给予参数s字符串,往后的调用则将参数s设置为NULL.
下面是程序例子:
#include  #include 
main()
{
    char s[ ]="ab-cd:de;gh:mnpe;ger-tu";
    char *delim="-:";
    char *p;
        printf("%s\n",strtok(s,delim));
    p=strtok(NULL,delim);
    while((p=strtok(NULL,delim)))
        Printf("%s\n",p);
}
输出结果为:
ab
cd
ee;gh
mnpe;ger
tu
一、sprintf
名称:
sprintf
功能: 
格式化字符串复制函数
头文件:
#include .h>
函数原形:
int sprintf(char *s,const char *format,.....);
参数: 
s       目的字符串数组
format   原字符字符串
返回值: 
成功返回参数str字符串的长度,失败返回-1
sprintf会把参数format字符串转换为格式化数据,然后将结果复制到参数str所指的字符数组,直到出现字符串结束符\0为止.
下面是程序例子:
#include 
#include 
main()
{
    struct tm *t;
    time_t timep;
    char buff[50];
    time(&timep);
    t=localtime(&timep);
    sprintf(buff,"%d:%d:%d",t->tm_hour,t->tm_min,t->tm_sec);
    printf("%s\n",buff);
}
运行结果:
12:33:21
二、atoi
名称:
atoi
功能: 
将字符串转换成整型数
头文件:
#include .h>
函数原形:
int atoi(const char *nptr);
参数: 
nptr   字符串 
返回值: 
返回转换后的整形数
atoi会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束符\0时结束转换,并将结束返回.
三、atof
名称:
atof
功能: 
将字符串转换成浮点型数
头文件:
#include .h>
函数原形:
double atoi(const char *nptr);
参数: 
nptr   字符串 
返回值: 
返回转换后的符点型数
atof会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束符\0时结束转换,并将结果返回.
#include 
main()
{
    char *a="100.23";
    char *b="200";
    float c;
    int d;
    c=atof(a);
    d=atoi(b);
    printf("c=%.2f\n",c);
    printf("d=%d\n",d);
}
运行结果:
c=-100.23
d=200
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值