C++ 字符串的 拼接,插入,查找与截取。
字符串的 拼接,插入,查找与截取。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<iostream>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string.h>
#define MAX 1024
using namespace std;
char * A(char *s1, char*s2)
{
strcat(s1, s2);
return s1;
}
char * B(char *s1,int a,int b )
{
char* buf = new char[MAX];
int j = 0;
for (int i = a; s1[i] && i < a+b; i++)
buf[j++] = s1[i];
buf[j] = '\0';
strcpy(s1, buf);
delete buf;
return s1;
}
char* C(char* s1, char* s2, int a) {
if (a > strlen(s1))
return s1;
char buf[2] = {0,0}, * p;
char* buf1 = new char[MAX];
buf[0] = s1[a];
s1[a] = '\0';
p = s1 + a + 1;
strcpy(buf1, s1);
strcat(buf1, s2);
strcat(buf1, buf);
strcat(buf1, p);
strcpy(s1, buf1);
delete buf1;
return s1;
}
int main()
{
int n,a,b;
cin >> n;
char s1[1024];
char s2[1024];
char* p;
getchar();
cin >> s1;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
switch (k)
{
case 1:
getchar();
cin >> s2;
cout << A(s1, s2) << endl;
break;
case 2:
cin >> a >> b;
cout << B(s1, a, b) << endl;
break;
case 3:
cin >> a;
getchar();
cin >> s2;
cout << C(s1, s2, a) << endl;
break;
case 4:
getchar();
cin >> s2;
p=strstr(s1, s2);
if(p)
cout << p - s1 << endl;
else
cout << -1 << endl;
}
}
return 0;
}