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



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

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

这应该可以解决问题。

最新更新