提示用户为首字母缩略词程序输入字符串

Prompting user to enter string for acronym program

该程序会提示用户输入一个短语,然后根据这些单词创建一个首字母缩写词。与具有大写字母的传统首字母缩略词不同,字母的大小写保持不变。如果我要输入 The Town Hall is old,我应该得到 TTHio。我如何使用 isspace 来确保我的程序正常运行,因为如果多个空格彼此相邻,它不会组合字符?它也不能处理制表符。

#include "stdafx.h"
#include <iostream> 
#include <string> 
#include <cctype> 
using namespace std; 
string create_acronym(string str); 
int main()
{
    cout << "This program tests the acronym function." << "\n"; 
    while (true)
    {
        cout << "\nPlease enter a string: "; 
        string str; 
        getline(cin, str); 
        if (str == "") 
        {
            break; 
        }
        cout << "\n\nThe acronym is \"" << create_acronym(str) << "\"" << "\n";
    } 
    return 0;
}
string create_acronym(string str) 
{
    string acronym = "";
    acronym = str.at(0); 
    for (int i = 0; i < str.length(); i++)
    {
        if (str.at(i) == ' ') 
        {
            acronym += str.at(i+1); 
        }
    }
    return acronym; 
}

只要记住你需要输出下一个非space:

string create_acronym( const string & str ) 
{
    string acronym;
    bool use_next = true;        

    for ( char c : str )
    {
        bool space = isspace(c);
        if ( use_next && !space ) acronym += c;
        use_next = space;
    }
    return acronym; 
}