首页 > 学院 > 开发设计 > 正文

7、字符串的输入与输出scanf、printf、cin、cout、gets、puts

2019-11-08 01:57:20
字体:
来源:转载
供稿:网友

1、scanf() 函数输入、PRintf() 函数输出

#include<stdio.h>;void main(){ char schar[100]; printf("Please input the string:(Length<100)/n"); //scanf("%s/n",schar); 后面加了 /n 后无法按回车正常结束字符串输入 scanf("%s",schar); printf("Ok, the first char is %c/n",schar[0]); printf("Ok, the string is %s/n",schar);}

结果: 这里写图片描述

2、cin 方式输入、cout 方式输出

#include<iostream>;using namespace std;void main(){ char schar[100]; cout<<"Please input the string:(Length<100)"<<endl; cin>>schar; cout<<schar[0]<<schar[1]<<schar[2]<<schar[3]<<schar[4]<<endl; cout<<schar<<endl;}

结果: 这里写图片描述

3、gets() 函数输入、puts() 函数输出

#include<stdio.h>;void main(){ char schar[100]; puts("Please input the string:(Length<100)"); gets(schar); puts("Ok, the string is:"); puts(schar);}

结果: 这里写图片描述

puts() 函数输出结果后自动换行,而 printf() 函数需要加上 “/n” 才换行


上一篇:多态

下一篇:合并排序数组

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表