您的位置:首页 > 其它

字符串函数

2017-08-25 13:17 190 查看
1、单字符的输入与输出

putchar
int puutchar(int c);
向标准输出写一个字符。
返回写了几个字符,EOF(-1)表示写失败。

getchar
int getchar(void);
向标准读入写一个字符。
返回类型是int是为了返回EOF(-1)。


#include<stdio.h>
void main()
{
int ch;
while((ch=getchar())!=EOF)
{
putchar(ch);
}
printf("EOF\n");
}


输入ctrl+z才结束。因为ctrl+z是自动结束。

strlen函数

size_t strlen(const char*s)
返回s的字符串长度(不包括0)


#include<stdio.h>
#include<string.h>
void main()
{
char line[]="hello";
printf("strlen=%lu\n",strlen(line));//长度
printf("sizeof=%lu\n",sizeof(line));//占据空间
}


strcmp函数

int strcmp(const char*s1,const char*s2);
比较两个字符串,返回
0:s1==s2
1:s1>s2
-1:s1<s2


#include<stdio.h>
#include<string.h>
void main()
{
char s1[]="hello";
char s2[]="hellp";
printf("%d\n",strcmp(s1,s2));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: