东方博宜OJ 1100:词组缩写 ← 字符串

【题目来源】
https://oj.czos.cn/p/1100
http://www.xiaopangoj.com/p/2420

【题目描述】
定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写。
比如,C 语言里常用的 EOF 就是 end of file 的缩写。

【输入格式】
测试数据占一行,有一个词组(总长度不超过 200),每个词组由一个或多个单词组成;每组的单词个数不超过 10 个,每个单词有一个或多个大写或小写字母组成;单词长度不超过 10,由一个或多个空格分隔这些单词。

【输出格式】
输出规定的缩写。

【输入样例】
end of file

【输出样例】
EOF

【数据范围】
每个词组总长度不超过 200,构成词组的单词不超过 10 个,每个单词长度不超过 10。

【算法分析】
要特判空串、或者字符串开头可能有空格的情况。否则,会出现“输出格式错误”。

【算法代码】

#include <bits/stdc++.h>
using namespace std;

const int maxn=205;
char t[maxn];
int cnt;

int main() {
    string s;
    getline(cin,s);

    if(!s.empty() && s[0]!=' ') {
        t[cnt++]=s[0];
    }

    for(int i=1; i<s.size(); i++) {
        if(s[i-1]==' ' && s[i]!=' ') {
            t[cnt++]=s[i];
        }
    }

    for(int i=0; i<cnt; i++) {
        if(t[i]>='a' && t[i]<='z') {
            cout<<(char)(t[i]-32);
        } else cout<<t[i];
    }

    return 0;
}

/*
in:end of file
out:EOF
*/




【参考文献】
https://blog.csdn.net/carbohydratesE/article/details/128392841
​​​​​​​https://blog.csdn.net/like_astar/article/details/128984236
https://blog.csdn.net/LYLYC_3/article/details/139996436


 

posted @ 2025-11-29 19:47  Triwa  阅读(11)  评论(0)    收藏  举报