如何输出字符串第二个单词的第一个字母?

时间:2019-04-18 11:54:08

标签: c++

我需要此代码的解决方案,它几乎完成了,但是我不明白如何从第二个单词而不是第一个单词中得到第一个字母?在此代码中,我得到第一个单词的第一个字母,并且不知道如何解决,要从用户输入的字符串的第二个单词中获得第一个字母。

#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;
}

希望您能理解我需要得到的东西。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

我建议您简要了解一下字符串类文档页面(http://www.cplusplus.com/reference/string/string/),其中有很多功能可以帮助您操作字符串。

根据此类中可用的功能(例如cbegin()cend()find()c_str()等),您可以执行以下操作:< / p>

#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,这是不正确的。

答案 2 :(得分:-2)

尝试一下:

#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;
}

这应该可以解决问题。

相关问题