簡體   English   中英

如何輸出字符串第二個單詞的第一個字母?

[英]How to output string second word first letter?

我需要這段代碼的解決方案,它快完成了,但我不明白如何從第二個單詞而不是第一個單詞中獲取第一個字母? 在這段代碼中,我得到第一個單詞的第一個字母,但不知道如何修復,從用戶輸入的字符串的第二個單詞中獲取第一個字母。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s, krt;
    int sk;
    cout << "Enter Array: ";
    getline(cin, s);
    sk = s.length();
    cout << "Character in string: " << sk << endl;
    int i, vst = 0, bas = 0;
    for (i = 0; i < sk; i++) {
        krt = s.substr(i, 1);
        if (krt == " ")
            vst = vst + 1;
    }
    cout << "Spaces count in string: " << vst << endl;
    char tpb;
    tpb = s[0];
    int pbk;
    pbk = tpb;
    cout << "String second word first letter: " << tpb << " and its ASCII code: " << pbk << endl;

    return 0;
}

希望你明白我需要得到什么。

主要問題是你使用0作為索引,而不是存儲你想要的索引; 空格后的第一個非空格字符。 至少,這是我要假設的定義 - 您沒有指定對多個連續空格做什么,也沒有指定包含非字母字符的字符串,例如“function()”,其中 vim 會說 '('是第二個單詞的第一個字符。擴展下面的代碼來做到這一點,留給讀者作為練習。

您的代碼中還有很多其他問題; 可以與定義連接的聲明,在只需要比較單個字符的情況下進行字符串比較,並在有算法可供選擇的情況下使用 for 循環。 這些加在一起給代碼增加了很多噪音。

最后,您需要一些代碼來處理沒有第二個單詞的情況,以免出現@Diodacus 代碼中的未定義行為。

使用常規 for 循環:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    cout << "Enter Array: ";
    getline(cin, s);
    const int sk = s.length();
    cout << "Character in string: " << sk << endl;
    int vst = 0;
    bool space_has_passed = false;
    int first_nonspace_after_space_index = -1;
    for (int i = 0; i < sk; i++) {
        if (s[i] == ' ') {
            vst = vst + 1;
            space_has_passed = true;
        } else if (space_has_passed && first_nonspace_after_space_index == -1) {
            first_nonspace_after_space_index = i;
        }
    }
    cout << "Spaces count in string: " << vst << endl;
    if ( first_nonspace_after_space_index != -1 && sk > first_nonspace_after_space_index) {
        const char tpb = s[first_nonspace_after_space_index];
        const int pbk = tpb;
        cout << "String second word first letter: " << tpb << " and its ASCII code: " << pbk << endl;
    } else {
        cout << "Need at least two words" << endl;
    }
    return 0;
}

如果使用<algorithms>則可以將其減少 7 行:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string s;
    cout << "Enter Array: ";
    getline(cin, s);
    const int sk = s.length();
    cout << "Character in string: " << sk << endl;
    auto first_space = std::find(s.begin(), s.end(), ' ');
    auto first_nonspace_afterspace = std::find_if(++first_space, s.end(), [](const char & c){ return c != ' '; });
    int count_spaces = std::count(s.begin(), s.end(), ' ');
    cout << "Spaces count in string: " << count_spaces << endl;
    if( first_nonspace_afterspace != s.end() ) {
        const char tpb = *first_nonspace_afterspace;
        const int pbk = tpb;
        cout << "String second word first letter: " << tpb << " and its ASCII code: " << pbk << endl;
    } else {
        cout << "Need at least two words" << endl;
    }
    return 0;
}

我建議你看一下字符串類文檔頁面( http://www.cplusplus.com/reference/string/string/ ),有很多函數可以幫助你操作字符串。

基於這個類提供的功能(如cbegin() cend() find() c_str()等等),你可以這樣做:

#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;

int main()
{
    string s;
    cout << "Enter array: ";
    getline(cin, s);
    int sk = s.length();
    cout << "Character in string: " << sk << endl;
    int vst = 0;
    for (auto i=s.cbegin(); i!=s.cend(); ++i)
        if(isspace(*i)) vst++;
    cout << "Spaces count in string: " << vst << endl;
    string t = s.substr(s.find(" ") + 1, 1);
    char tpb = *t.c_str();
    int pkt = tpb;
    cout << "String second word first letter: " << tpb << " and its ASCII code: " << pkt << endl; 
    return 0;
}

主要問題是你打印了傳入參數的字符串的第一個字母:

tpb = s[0];

你應該:

  • 記住第二個單詞的第一個字符的位置索引

或者

  • 獲取傳入參數的字符串的第二個單詞並打印該字符串的第一個字符

最后,當只傳遞一個詞時會發生什么?

你也應該考慮一下。 在上面的代碼中,如果您通過單詞test程序無論如何都會打印String second word first letter: t and its ASCII code: 116這不是真的。

嘗試這個:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string s, krt;

    int sk;
    cout << "Enter Array: ";
    getline (cin, s);
    sk = s.length();
    cout << "Character in string: " << sk << endl;

    int i, vst = 0, bas = 0; 
    for (i = 0; i < sk; i++)
    {
        krt = s.substr(i, 1);
        if (krt == " ") vst = vst + 1;
    }
    cout << "Spaces count in string: " << vst << endl;

    char tpb;
    for (i = 0; i < sk; ++i) // Searching for first space
    {
        if (s[i] == ' ')
            break;
    }
    tpb = s[i + 1];

    int pbk;
    pbk = tpb;
    cout << "String second word first letter: " << tpb << " and its ASCII code: " <<           pbk << endl;

    return 0;
}

這應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM