你了解gets()和scanf()吗

gets()和scanf()联系:都可以用于输入字符串;

1、scanf()

语法:scanf("格式控制字符串",变量地址列表);

接受字符串时:scanf("%s",字符数组名或指针);

2、gets()

语法:gets(字符数组名或指针);

区别:scanf遇到空格、回车和Tab键都会认为输入结束,所有它不能接收空格;

gets遇到回车认为输入结束,可以接收空格。

下面丢出一个程序:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 int main()
 5 {
 6     int i=0,j=0;
 7     char s1[80],s2[40];
 8     printf("input string1:");
 9     gets(s1);//遇到回车认为输入结束
10     //scanf("%s",s1);//遇到空格认为输入结束
11     printf("input string2:");
12     gets(s2);
13     //scanf("%s",s2);
14     /*strcat(s1,s2);//直接用strcat函数实现
15     puts(s1);*/
16     while(s1[i]!='')
17         i++;
18     while(s2[j]!='')
19         s1[i++]=s2[j++];
20     s1[i]='';
21     puts(s1);
22     //printf("
new string is:%s
",s1);
23     system("pause");
24     return 0;
25 }
原文地址:https://www.cnblogs.com/crystalmoore/p/5924328.html