linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > 软件开发 >
栏目导栏
资料搜索
热门文章
·Linux下的通用线程池创建
·Linux 下 C 语言编程
·C++字符串转换篇
·linux C 进程操作篇
·linux C 文件权限控制篇
·linux上的C/C++编译器gcc/egcs
·linux C 接口处理篇
·GCC使用手册
·在Redhat Linux上安装 GCC 编译
·GCC使用指南
·Scheme 语言介绍
·Linux下C开发环境的构成和安装
·GCC使用手册与常用命令
·C语言运算符
·Linux上搭建C/C++IDE开发环境
最新文章
·linux c 一个autotools的最简单
·linux 和windows 线程间的区别
·Linux下Rtl8139too网卡设备驱动
·Linux系统的驱动框架及驱动加载
·Linux环境下的高级隐藏术
·在Windows系统下配置Linux C++
·Linux pthread 编程
·Fedora7下安装JDK6 Eclipse3.3
·linux套接字编程常用函数
·Linux操作系统设置OpenGL的编程
·JDK1.4和JDK1.5在Linux下的中文
·Ubuntu下两个项目管理工具Dia和
·Linux串口编程分析
·在Ubuntu下建立PowerPC405EP的
·程序员的VI编辑器简明教程
linux c常用字符串处理函数1
[ 作者:  加入时间:2007-10-11 11:29:29  来自:Linux联盟收集整理 ]

一、puts

名称:

puts

功能: 

向显示器输出字符串。

头文件:

#include <stdio.h>

函数原形:

int puts(const char *s);

参数

s    字符串   

返回值: 

成功返回输出的字符数,失败返回EOF

put函数与printf函数在字符串输出中的区别:

puts在输出字符串时,遇到'\0'会自动终止输出,并将'\0'转换为'\n'来输出。

Printf在输出字符串时,遇到'\0'只是终止输出,并不会将'\0'转换为'\n'来输出。

二、strcat

名称:

strcat

功能: 

字符串连接函数

头文件:

#include <string.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 <string.h>

函数原形:

char *strcpy(char *restrict s1,const char *restrict s2);

参数

s1    字符串1

s2    字符串2

返回值: 

返回字符数组1的首地址

strcpy将字符串2,拷贝到字符数组1中去,要注意,字符数组1必须足够大,拷贝时'\0'一同拷贝,不能使用赋值语句为一个字符数组赋值。

四、strcmp

名称:

strcmp

功能: 

字符串比较函数

头文件:

#include <string.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 <stdio.h>

#include <string.h>

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 <string.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 <string.h>

函数原形:

char *strtok(char *s,const char *delim)

参数

s      欲分割的字符串

delim  分割字符串

返回值: 

返回下一个分割后的字符串指针,如果已无从分割则返回NULL

Strtok可将字符串分割,当strtok在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为\0字符。

在第一次调用时,strtok必须给予参数s字符串,往后的调用则将参数s设置为NULL.

下面是程序例子:

#include <stdio.h> #include <string.h>

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

评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·linux c常用字符串处理函数2  (2007-10-11 11:30:39)
 ·用Linux Command Shell模拟DOS命令  (2007-01-25 11:32:32)
 ·可用于G03并行计算的linux cluster安装手册完全版  (2006-12-19 13:05:25)
 ·Linux Cluster  (2006-12-19 13:01:59)
 ·Google Linux Cluster的系统结构分析  (2006-12-19 13:00:13)
 ·如何在Linux console中直接显示中文  (2006-09-05 10:25:14)
 ·Linux C/C++软件开发工程师(赴IBM)  (2006-08-21 09:49:01)
 ·完全支持NTFS 读写的Linux 驱动发布  (2006-07-18 11:43:50)
 ·Arm Linux C++软件工程师(Arm Linux C++ Software Engineer)  (2006-07-14 08:05:15)
 ·linux c 终端控制篇  (2006-06-24 14:21:40)