自行设计函数实现字符串复制

【问题描述】
字符串复制。
输入一个字符串t和一个正整数m,将字符串t中从第m个字符开始的全部字符复制到字符串s中,再输出字符串s。
要求用字符指针定义并调用函数strmcpy(s,t,m),它的功能是将字符串t中从第m个字符开始的全部字符复制到字符串s中。

【输入形式】
首先打印提示"Input a string:";然后直接在冒号后面输入字符串,作为t的内容,字符串中可以包含空格;字符串以回车结束。
打印提示"Input an integer:";然后直接在冒号后面输入一个正整数,代表m的值;回车。

【输出形式】
首先打印"Output is:";紧跟后面输出字符串s中的内容;换行。

【运行时的输入输出样例】
Input a string:happy new year
Input an integer:7
Output is:new year

【提示】
第4组测试数据,当输入的整数m为0,不是合法的正整数时,输出0

#include <iostream>
#include <string.h>
using namespace std;

void strmcpy(char * s, char *t, int m);

int main()
{
    char s[100],t[100];
    int m;
    cout << "Input a string:";
    cin.get(t,100);
    cin.get();
    cout << "Input an integer:";
    cin  >> m;
    if(m>0)
    {
        strmcpy(s,t,m);
        cout << "Output is:" << s << endl;
    }
    else cout << "Output is:0" << endl;
    return 0;
}

void strmcpy(char * s, char *t, int m)
{
    int i,len = strlen(t)-m+1;//len是剩下要复制字符串的长度
    for(i=0;i<len;++i,++m)
        s[i] = t[m-1];
    s[i] = '';//别忘记在末尾添加上结束标记符
}

原文地址:https://www.cnblogs.com/yuzilan/p/10626134.html