首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建字符串文本

创建字符串文本
EN

Stack Overflow用户
提问于 2019-10-08 06:39:27
回答 1查看 38关注 0票数 1

我有一个字符串数组。我想取数组中的第n个单词,并打印如下输出:

输入:government输出:g8t

基本上:first letter + total number of letters-2 + last letter

输出也存储在字符串数组中。下面是我尝试过的基本代码:

代码语言:javascript
复制
out[j]=in[j].at(0) + (n-2) + in[j].at(n-1);
//n is the length of word , used str.length() for that

这里怎么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-08 09:45:25

to_string

小规模的审判:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58281263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档