1、对《算法笔记》里的练习题注意点做一个总结
2、字符串的输入输出,各种方法细节混杂,可以说快成了老大难问题了。所以今天要在这里完全搞懂
3、小结
C++
:使用cin.getline(str, MAX_LEN)
。C语言
:使用gets_s(str, MAX_LEN)
。想玩花活的(非必要不建议)
:使用scanf("%[^\n]", str)
或fgets(str, MAX_LEN, stdin
4、
cin.getline(str, MAX_LEN);
//添加头文件#include <iostream>; using namespace std;
5、
gets_s(str, MAX_LEN)
//c语言用
6、
fgets(str, MAX_LEN, stdin);
//c++和c都可以用
//会把输入的换行符也读到字符数组里面,因此对于要读入多行字符串的情况,就会很麻烦,会需要一些额外处理
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = '\0';
//其中,strcspn函数需要#include <string.h>,含义是在一个数组中寻找某个或某些字符
7、最好的字符串输入输出
//c++推荐
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 51;
char str[MAXN];
int main() {
cin.getline(str, MAXN);
printf("%s", str);
return 0;
}
8、
//C语言推荐
#include <cstdio>
const int MAXN = 51;
char str[MAXN];
int main() {
gets_s(str, MAXN);
printf("%s", str);
return 0;
}