本文发布于 1587 天前,最后更新于 1457 天前,其中的信息可能已经有所发展或是发生改变。
#include <bits/stdc++.h>
//1. 编写函数,取一个字符串的子串。
using namespace std;
string GetSubstring(string s,int a,int b)
{
string ans(s,a,b);
return ans;
}
int main()
{
string s;
int a,b;
cout << "Input string"<<endl;//输入字符串
cin >> s;
cout << "Enter the substring subscript range to be obtained (the following table starts from 0)"<<endl;//输入要获取的子串下标范围(下表从0开始)
cin >> a >> b;
string ans = GetSubstring(s,a,b);
cout << ans;
}
#include <bits/stdc++.h>
using namespace std;
//2. 编写函数,把一个字符串插入到另一个字符串中指定位置。
void AppSubstring(char *&s_1,int a,char *s_2)
{
char *ans;
int len = sizeof(s_1) + sizeof(s_2);
int llen = 0;
ans = new char[len];
for(int i=0; i<=len; i++)
{
if(i==a+1)
for(unsigned int ii=0; ii<strlen(s_2); ii++)
ans[llen++] = s_2[ii];
ans[llen++]=s_1[i];
}
delete s_1;
s_1 = ans;
}
int main()
{
char *s_1="abcfgh";
char *s_2="de";
int a = 2;
AppSubstring(s_1,a,s_2);
cout << s_1;
}
#include <bits/stdc++.h>
using namespace std;
//3. 编写函数,求一个字符串中不重复字符组成的字符串,如字符串“dasasew”的不重复字符串为“dasew”。(选做)
string NotRepeatingSubstring(string s)
{
bool PdAscii[256];
memset(PdAscii,0,sizeof(PdAscii));
char ans[256];
int n=0;
for(int i=0; i<=s.length(); i++)
{
if(!PdAscii[s[i]])
{
PdAscii[s[i]]=1;
ans[n++]=s[i];
}
}
return ans;
}
int main()
{
string s;
cin >> s;
string ans = NotRepeatingSubstring(s);
cout << ans;
}