字符串插入的简单实现

描述
有两个字符串str和substr,str的字符个数不超过10,substr的字符个数为3。(字符个数不包括字符串结尾处的’\0’。)将substr插入到str中ASCII码最大的那个字符后面,若有多个最大则只考虑第一个。(来源:POJ)

输入
包括若干行,每一行为一组测试数据。
输出
输出插入之后的字符串

样例输入

abcab eee
1234561 789

样例输出

abceeeab
1234567891

示例程序

//Data:June 19 ,2017
//Writen by Yuxin Liu
//在不考虑字符串相关操作函数的情况下,实现简单的字符串插入功能。
#include 
using namespace std;
int main()
{
    char str[13] = {};
    char substr[3] = {};
    char temp[10] = {};//用于存储临时字符串变量

    while (cin>>str)
    {
        cin >> substr;
        //max是指str字符串中的最大字符,j用于存储字符在字符串str中的索引。
        int max = 0,j=0;
        //遍历寻找最大字符,及其索引。
        for (int i = 0; i < 13; i++)
        {
            if (str[i]>max)
            {
                max = str[i];
                j++;

            }
        }
        int m = j;//考虑j的用途,将索引另赋值于m
        //将str字符串中最大字符后面的字符转存在temp中,用于后续补充。
        for (int i = 0; i < 13-j; i++)
        {
            temp[i] = str[j];
            j++;
            if (str[j] == '\0')
                break;
        }
        //将substr插入在str最大字符后面
        for (int i = 0; i < 3; i++)
        {
            str[m + i] = substr[i];
        }
        //将转存在temp的字符,再赋给str。
        for (int i = 0; temp[i]!='\0'; i++)
        {
            str[m + i + 3] = temp[i];
        }
        cout << str << endl;
    }
    return 0;
}

你可能感兴趣的:(C++程序实现——学习阶段,c++学习,字符串插入简单实现)