1)写出存储字符串的接口,返回字符串编号; 2)写出根据字符串编号读取字符串的接口。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<bitset>
#include<fstream>
#include<sstream>
using namespace std;

/*
程序设计题。动态数组。
有未知长度的字符串,有大小为N的空间,用户输入字符串并将字符串存入空间中。
第一次存入编号为1,第二次存入编号为2,以此类推。
要求
1)写出存储字符串的接口,返回字符串编号;
2)写出根据字符串编号读取字符串的接口。
*/

#define N 10000

char space[N];
int current_index;
vector<int> index;

int StoreAndGetStringNumber(char* ch) {
int count = 0;
while (*ch != '\0') {
space[current_index++] = *ch++;
count++;
}
index.push_back(count);
return index.size() - 1;
}

char* GetString(int number) {
int offset = 0;
for (int i = 0; i != number; i++)
offset += index[i];
char* ch = new char[index[number] + 1];
ch[index[number]] = '\0';
for (int i = 0; i != index[number]; i++)
//cout << space[i];
ch[i] = space[i + offset];
return ch;
}

int main()
{
char p1[] = "111111111111";
char p2[] = "222222";
char p3[] = "333";
StoreAndGetStringNumber(p1);
StoreAndGetStringNumber(p2);
StoreAndGetStringNumber(p3);
char* c = GetString(2);//此处选择下标
for (int i = 0; c[i] != '\0'; i++)
cout << c[i];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值