我有一个字符串数组。我想将第n个单词放在该数组中,并打印如下输出:
输入:government
输出:g8t
基本上:first letter
+ total number of letters-2
+ last letter
输出也存储在字符串数组中。 这是我尝试过的基本代码:
out[j]=in[j].at(0) + (n-2) + in[j].at(n-1);
//n is the length of word , used str.length() for that
这是怎么了?
答案 0 :(得分:0)
注意这一点 to_string !!!
一个小试验:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
vector<string> words;
string input;
int count = 0;
cout << "Enter 5 words :";
while (cin >> input)
{
words.push_back(input);
if (++count > 4)
break;
}
for (int i = 0; i < words.size(); i++) {
string str;
int size = words.at(i).length();
str = words.at(i).at(0) + to_string(size) + words.at(i).at(size - 1);
cout << str + "\n";
}
cin>> input;
return 0;
}