字符串的多组输入格式
scanf("%c", &ch)
读取单个字符,用EOF
作为结束的判断标志。
刷题记录:[题] 查找最大元素 #字符输入
逐个字符手动读取,因为题目的要求,要对每个字符逐个操作,所以就输入的时候顺便比较大小了。
我的理解是:每个字符串末尾会有'\0'
字符,然后整个所有组的输入都齐了之后会有一个EOF
。
#include<bits/stdc++.h>
using namespace std;
int main() {
char s[100000];
while(scanf("%c", &s[0]) != EOF) {
}
return 0;
}
gets(str)
读取char[N]
定义的字符串,不用加结束条件进行判断。
能读空格,遇到换行才结束。
刷题记录:[题] 首字母变大写 #字符输入
#include<bits/stdc++.h>
using namespace std;
char str[101];
int main() {
while(gets(str)) {
}
return 0;
}
从下面例子可以看到,这样读进去的字符串,会在最后面加上
'\0'
#include<bits/stdc++.h>
using namespace std;
char str[101];
int main() {
gets(str);
for(int i = 0;;i ++) {
if(str[i] == '\0') {
printf("遇到'\\0'了,在第%d位这里", i);
break;
}
else
printf("%c", str[i]);
}
return 0;
}
getline(cin, s)
读取string
定义的字符串,不用加结束条件进行判断。
能读空格,遇到换行才结束。
刷题记录:[题] 首字母变大写 #字符输入
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
while(getline(cin, s)) {
}
return 0;
}
从下面例子可以看到,这样读进去的字符串,会在最后面加上
'\0'
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
getline(cin, s);
for(int i = 0;;i ++) {
if(s[i] == '\0') {
printf("遇到'\\0'了,在第%d位这里", i);
break;
}
else
printf("%c", s[i]);
}
return 0;
}