gets():以回车为结束符,格式gets(字符数组名)
scanf_s("%s",&str); 以空格、制表符、换行符为结束符
格式:scanf_s("%s",字符数组名);
输入 abcedf 123456\n
gets(str):str[]为abcedf 123456\0
scanf_s("%s",str);str[]为abcedf\0
输入 abcedf\n 123456
gets(str),ch=getchar(str);ch:‘1’
scanf_s("%s",str); ch=getchar(); ch为'\n'
字符串的输出
puts():自动在字符串末尾输出回车,格式puts(字符数组名)
printf():格式printf("%s",字符数组名);
//字符串的输入输出
gets()和puts();
#include<stdio.h>
#define N 100
void main(){
char str[N];
printf("this input string is:");
gets(str);
printf("The result is:");
puts(str);
}
scanf_s()和printf();
#include<stdio.h>
#define N 100
void main() {
char str[N];
printf("please input some char:\n");
scanf_s("%s",str);
printf("the result is:%s\n",str);
}
字符串连接函数:strcat()
格式:strcat(s1,s2)
功能:将字符串s2连接到字符串s1的后面
说明
>s1是字符数组名或字符数组的开始地址
s2既可以是字符数组名,也可以是字符串
>函数执行之后,s1是连接之后的字符串,s2保持不变
>s1长度应不小于两个字符串的长度之和
#include <stdio.h>
#include<string.h>
int main(void) {
char c1[20]="China",c2[10]="man";
strcat(c1, c2);
printf("output:c1\n");
puts(c1);
printf("output:c2\n");
puts(c2);
return 0;
}