getline
input.txt文件里面有一段文章,由字符串和一些特殊字符构成。先把 input 文件中的内容读入缓冲区,在从缓冲区中取字符,进行如下操作:
如果是字符,空格,输出
如果是!,删除前面一个字符
如果是*,删除前面 1 行字符串
如果是>,讲前面一个单词的首字符,进行大小写转化
如果是数字,则不作任何操作
输入示例:
this is some > text!
one plus one eqs 2
this line should be deleted
prev line sh*ould be deleted
输出示例:
this is Some tex
one plus one eqs
prev line should be deleted
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
int main(int argc, char *argv[]) {
list<string> lines;
string tmp;
ifstream ifs("./input.txt");
while (getline(ifs, tmp))
lines.push_back(tmp);
for (auto it = lines.begin(); it != lines.end(); it++) {
for (int i = 0; i < it->length(); i++) {
if (it->at(i) == '!' && i > 0)
(*it)[i-1] = '0';
else if (it->at(i) == '*')
lines.erase(prev(it));
else if (it->at(i) == '>') {
int j;
for (j = i-1; j >= 0 && it->at(j) != ' '; j--);
for (; j >= 0 && it->at(j) == ' '; j--);
if (j < 0)
continue;
for (; j >= 0 && it->at(j) != ' '; j--);
(*it)[j+1] = toupper((*it)[j+1]);
}
}
}
for (auto &s : lines) {
for (auto c : s) {
if (isalpha(c) || c == ' ')
cout << c;
}
cout << endl;
}
return 0;
}