1.字符串的输入和输出
- scanf(格式控制字符串,输入参数表);
例:scanf(“%s”,s);(该函数遇回车和空格输入结束)
- printf(格式控制字符串,输出参数表);
例:printf(“%s”,s);
- gets(s)(输入的字符允许带空格)
- puts(s)(输出时遇’\0’自动将其转化为’\n’)
2.字符串的复制,连接和比较及字符串的长度
- 字符串复制函数char*strcpy(char*s1,char*s2)
参数s1必须是字符型数组基地址,参数s2可以是字符数组名或字符串常量
char s1[80],s2[80],a[80]=”hello”;
strcpy(str1,a); //把a中的字符串复制给str1
strcpy(str2,”world”);//把字符串常量“world”复制给str2
- 字符串连接函数strcat(s1,s2)
char str1[80]=”hello”,str2[80],a[80]=”world”;
strcat(str1,a);//连接str1和a,结果为hello world
strcpy(str2,str1);//将str1中的字符串赋给str2
strcat(str2,”!”);//结果为hello world!
- 字符串比较函数strcmp(s1,s2)
strcmp(“sea”,”sea”)的值为0,”sea’与”sea”相等
strcmp(“compute”,”compare”)的值(‘u’-‘a’)是个正数
strcmp(“happy”,”z”)的值(‘h’-‘z’)是个负数
strcmp(“sea”,”seat”)的值(‘\0-‘t’)是个负数
- 字符串长度strlen(s1)例
strlen(“happy”)的值是5
p.s.
在应用标准库中的任何函数之前,必须提供函数原型。#include<string.h>
—–部分内容参考《c语言程序设计》(第三版)