C与C++中输入字符串的方法
来源:互联网 发布:网络优化论文3000字 编辑:程序博客网 时间:2024/05/23 12:24
1、C语言中输入字符串
(1)使用scanf_s函数
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
intmain()
{
char a[10], b[10];
scanf_s("%s", a, sizeof(a)); //要取地址符&和不要都可以
scanf_s("%s", &b,sizeof(b));
printf("输入的字符串a是:%s\n", a);
printf("输入的字符串b是:%s\n", b);
int c;
scanf_s("%d", &c);
return0;
}
(2)使用gets_s()函数#include "stdafx.h"
#include <stdio.h>
#include "string.h"
#include<stdlib.h>
intmain()
{
char a[10], b[10];
//scanf_s("%s", a, sizeof(a)); //要取地址符&和不要都可以
//scanf_s("%s", b,sizeof(b));
//printf("输入的字符串a是:%s\n", a);
//printf("输入的字符串b是:%s\n", b);
gets_s(a);
printf("输入的字符串a是:%s\n", a);
system("pause");
return0;
}
2、C++中输入字符串的方法
(1)使用getline()函数
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
intmain()
{
char a[10], b[10];
cin.getline(a,10);
cin.getline(b,10);
cout<< "a字符串的是:"<<a<< endl;
cout<< "b字符串的是:"<<b<< endl;
int d=0;
cin>> d;
return 0;
}
(2)使用get()函数
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
intmain()
{
char a[10], b[10];
cin.get(a,10);
cin.get(b,10);
cout<< "a字符串的是:"<<a<< endl;
cout<< "b字符串的是:"<<b<< endl;
int d=0;
cin>> d;
system("pause");
return 0;
}
(3)使用string类
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
intmain()
{
/*char a[10], b[10];
cin.get(a,10).get();
cin.get(b,10);
cout<< "a字符串的是:"<<a <<endl;
cout<< "b字符串的是:"<<b <<endl;
intd=0;
cin>> d;*/
string str;
getline(cin,str);
cout<< "字符串str为:"<<str<< endl;
system("pause");
return 0;
}
- C与C++中输入字符串的方法
- C与C++中字符串的输入
- C语言中字符串的输入问题
- 字符串的输入--C
- 详解C与C++中操作字符串方法的不同
- C语言中基础的字符与字符串的输入与输出
- C语言中字符串输入中scanf与gets函数的区别
- C语言中输入包含空格字符串的两种方法
- C/C++字符串的输入
- 标准C中字符串分割的方法
- 对输入的字符串中C关键词的查找统计。
- C/C++中带空格的字符串的输入
- C-风格字符串与面向行输入的一些要点
- C语言字符串输入函数的比较与解析
- c中输入不固定长度的字符串
- C语言中关于字符串的输入问题
- C/C++中输入带空格的字符串用法总结
- 黑马程序员---C语言中字符串的输入
- 初恋
- c语言中auto、register、extern、static用法
- 算法设计周记(四)--回溯
- js快速排序
- canvas入门学习
- C与C++中输入字符串的方法
- 传输层两大协议:TCP与UDP详解(两者的联系与区别)
- vi和vim入门(二)之查找替换
- 项目工程结构搭建
- 【广告算法工程师入门 14】机制设计-带有保留价的拍卖与最优保留价
- logistic回归与正则化
- Day79
- http状态码总结
- Kali Linux 创建用户