Codeforces Testing Round #4 A. Punctuation 分割单词

题意:

给定字符串,把其中的单词和标点符号隔开,需要满足以下3条规则:

1 如果两个单词之间没有标点符号,则它们应该分隔一个空格
2 每个标点符号前应该没有空格

3 每个标点符号后应该有一个空格

思路:

将每个单词、标点符号作为string存起来,然后输出的时候判断当前位置后面是否能够添加空格;


#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 7;
char s[maxn];
vector<string> v;

int main() {
    gets(s);
    int n = strlen(s);
    for(int i = 0; i < n ; ) {
        if(s[i]>='a'&&s[i]<='z') {
            string t;
            while(s[i]>='a'&&s[i]<='z') {
                t.push_back(s[i]); i++;
            }
            v.push_back(t);
        }
        else if(s[i] == ' ') {
            i++;
        }
        else {
            string t;
            t.push_back(s[i]);i++;
            v.push_back(t);
        }
    }
    for(int i = 0; i < v.size();) {
        cout<<v[i]; i++;
        if(i >= v.size()) break;
        if(v[i][0] >= 'a' && v[i][0] <= 'z') {
            cout << " ";
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/80483568
今日推荐