您的位置:首页 > 其它

字符串的输入问题(如果输入的字符串带有空格)

2015-08-04 14:35 423 查看
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a[126];
cout<<"1 please input the str"<<endl;
cin>>a;
int n1=strlen(a);
cout<<"n1:"<<n1<<endl;
cout<<"str[]:"<<a<<endl;
system("pause");
return 0;
}




#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cout<<"1 please input the str"<<endl;
cin>>str;
int n2=str.length();
cout<<"n2:"<<n2<<endl;
cout<<"string str:"<<str<<endl;
system("pause");
return 0;
}




#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[126];
cout<<"3 please input the str"<<endl;
scanf("%s",str);
int n2=strlen(str);
cout<<"n2:"<<n2<<endl;
cout<<"string str:"<<str<<endl;
system("pause");
return 0;
}




#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[126];
cout<<"3 please input the str"<<endl;
gets(str);
int n2=strlen(str);
cout<<"n2:"<<n2<<endl;
cout<<"string str:"<<str<<endl;
system("pause");
return 0;
}




综上,要想输入带' '空格的字符串,需要使用gets()函数才行, cin scanf 都不可以,会受到空格的影响。

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