截取字符串(函数)

82 阅读1分钟

题目描述

编写程序,其作用是,将从字符串1 (长度超过30) 的第index个字符开始的所有字符复制,生成新的字符串2。

输入

测试数据的组数n

第一组数据

第二组数据

........

输出

成功生成就输出子串,不成功生成,输出"IndexError"

输入样例

3
Zhenshen University
9
www.szu.edu.cn
12
apple
8

输出样例

University
cn
IndexError

代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int t,i;
	string a;
	cin>>t;
	while(t--)
	{
		getchar();
		getline(cin,a);
		int index;
		cin>>index;
		if(index>=a.size())
		cout<<"IndexError"<<endl;
		else
		{
			for(i=index;a[i];i++)
			cout<<a[i];
			cout<<endl;
		}
	}
}