0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AtCoder】ABC 085A split関数を自作したバージョン

Posted at
#include<iostream>
#include<string>
#include<vector>

using namespace std;

vector<string> split(string str, char delim) {
    int first = 0;
    // strはinput文字。delimは区切り文字。
    // 最初の区切り文字の位置を取得
    int last = str.find_first_of(delim);
    // 分割した文字列を格納するvector
    vector<string> result;

    while (first < str.size()) {
        // 入力文字strのfirstからlast-first文字を取得
        string subStr(str, first, last - first);
        // 取得した文字列を格納
        result.push_back(subStr);
        // 次の区切り文字の位置を取得
        first = last + 1;
        // 区切り文字がfirstの位置以降で現れる位置を探す
        last = str.find_first_of(delim, first);
        if (last == string::npos) {
            last = str.size();
        }
    }
    return result;
}

int main() {
    string s;
    cin >> s;

    // 分割対象の文字列
    char delim = '/';

    vector<string> parts = split(s, delim);
    if (parts[0] != "2018") {
        parts[0] = "2018";
    }

    for (int i = 0; i < parts.size(); i++) {
        cout << parts[i];
        if (i < parts.size() - 1) {
            cout << "/";
        }
    }
    cout << endl;

    return 0;
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?