我有一个字符串数组。我想取数组中的第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这里怎么了?
发布于 2019-10-08 09:45:25
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;
}https://stackoverflow.com/questions/58281263
复制相似问题