课评: 转出学生归档(王子墨/张立坤/黄俊博)+ 周四班级总结 + 新生章梓皓档案 + C++示例代码
This commit is contained in:
46
C++代码/GESP考级/三级/202609分割字符串.cpp
Normal file
46
C++代码/GESP考级/三级/202609分割字符串.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
string s;
|
||||
|
||||
int main(){
|
||||
getline(cin, s);
|
||||
|
||||
int len = s.size();
|
||||
string curr = ""; // 当前这个字符串
|
||||
int k = 1; // 需要多少个空格
|
||||
int idx = 0; // 当前的下标
|
||||
|
||||
while(idx < len){
|
||||
char c = s[idx];
|
||||
if(c == ' '){
|
||||
// 当遇到空格
|
||||
string z = s.substr(idx, k);
|
||||
bool ok = true;
|
||||
for(char c1 : z){
|
||||
if(c1 != ' ') {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(ok){
|
||||
cout << curr << endl;
|
||||
idx += k;
|
||||
k++; // 下一次的空格要加一
|
||||
curr = "";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
curr += c;
|
||||
if(idx == len - 1){
|
||||
cout << curr << endl;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user