Tsukinai的第七十个程序(一个字符串插入至另一个源字符串的某个位置)

Tsukinai的第七十个程序

将一个字符串插入至另一个源字符串的某个位置:
将一个字符串2插入到源字符串1中 第一次出现某字符的位置,并打印出形成的新串。
如果 字符串1中找不到输入的字符, 则显示“Not found!”并结束程序。
注:源字符串长度及待插入字符串长度不超过50

提示信息:
printf(“Input source string 1:\n”)
printf(“Input inserted string 2:\n”)
printf(“Input a letter to locate the index:\n”)

输出信息格式:
printf(“The new string is:%s”)
printf(“Not found!”)

测试样例1:
输入信息:
Input source string 1:
abcdecfg
Input inserted string 2:
---
Input the a letter to locate the index:
c
输出结果:
The new string is:ab*---*cdecfg

测试样例2:
输入信息:
Input source string 1:
abcdecfg
Input inserted string 2:


Input the a letter to locate the index:
h
输出结果:
Not found!

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    int i, len1, len2, flag = 0, x;
    char a[40], b[40], c[80], d;
    printf("Input source string 1:\n");
    gets(a);
    len1 = strlen(a);
    printf("Input inserted string 2:\n");
    gets(b);
    len2 = strlen(b);
    printf("Input a letter to locate the index:\n");
    scanf("%c", &d);
    for (i = 0; i < len1; i++)
    {
        if (a[i] == d)
        {
            flag = 1;
            x = i;
            break;
        }
    }
    if (flag)
    {
        for (i = 0; i < x; i++)
        {
            c[i] = a[i];
        }
        for (i = 0; i < len2; i++)
        {
            c[i + x] = b[i];
        }
        for (i = x; i < len1 + len2; i++)
        {
            c[len2 + i] = a[i];
        }
        printf("The new string is:%s", c);
    }
    else
        printf("Not found!");
    system("pause");
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值