题目描述
从键盘输入任意的一个字符串A ,其长度L 不小于6 ,同时再输入一个整数N (其中:N<L)作为插入点,以及任意的一个字符串B ,其长度为L1 (其中:L1<L ),现要求完成下列功能:
如果字符串A 的长度L 大于100 输出 100 ,否则输出L ;
在N 点处,插入字符串B 后,将得到一个新字符串C ,请计算并输出最后的字符串;
查找字符串C 中,从右到左第一个出现字母 x 的位置。
输入
输入包括三行:
第一行:长度为L 的字符串A(5≤L≤10000) ;
第二行:一个整数N ,作为字串的插入点(N<L );
第三行:长度为L1 的字符串B (其中:L1<L )。
输出
第一行:如果字符串A 的长度L 大于100 输出 100,否则输出L ;
第二行:新字符串 C ;
第三行:字符串 C 中,从右到左第一个出现字母 x 的位置。
样例输入1
AAAAAA
2
xxx
样例输出1
6
AxxxAAAAA
6
#include <stdio.h>
#include <string.h>
void insert(char s1[], char s2[], int n) {
char s[100000];
int i,j,k,num = 0;
for (i = 0; i < n - 1; i++) {
s[k++] = s1[i];
}
for (j = 0; j < strlen(s2); j++) {
s[k++] = s2[j];
}
for (;i < strlen(s1);i++) {
s[k++] = s1[i];
}
if (strlen(s1) > 100) printf("100\n");
else printf("%d\n",strlen(s1));
printf("%s\n",s);
for (;k > 0; k--,num++) {
if (s[k] == 'x') {
printf("%d\n", num);
break;
}
}
}
int main() {
char str1[10000],str2[100000];
int n;
scanf("%s", str1);
scanf("%d", &n);
scanf("%s", &str2);
insert(str1, str2, n);
return 0;
}
#include <iostream>
#include<string>
using namespace std;
int main() {
string a, b;
int n;
cin >> a >> n >> b;
if (a.size() > 100) cout << 100 << endl;
else cout << a.size() << endl;
string c = a.insert(n - 1, b);
cout << c << endl;
for (int i = c.size() - 1,j = 1; i >= 0; i--,j++){
if (c[i] == 'x') {
cout << j << endl;
break;
}
}
return 0;
}