讲一个字符串从第m个字符开始复制到另一个字符串并输出

我的运行结果room前面有空格

img


题目是这样的:

img


我写的程序如下:
#include <stdio.h>
#include <string.h>
int main()
{
void copystr(char *from,char *to,int m);
char a[20],b[20];
int m;
printf("请输入字符串:");
gets(a); //输入
printf("请输入m的值:");
scanf("%d",&m);
copystr(a,b,m); //调用
printf("另一个字符串为:%s",b);
}
void copystr(char *from,char to,int m)
{
int i;
for(i=m-1;
(from+i)!='\0';i++)
(to+i)=(from+i);
*(to+i)='\0';
}
希望帮忙看看怎么样才能前面不显示空格

from字串的指针先移动m-1个位置,再开始复制:

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

void copystr(char *from,char *to,int m);

int main()
{
    char a[20],b[20];
    int m;
    printf("请输入字符串:");
    gets(a); //输入
    printf("请输入m的值:");
    scanf("%d",&m);
    copystr(a,b,m); //调用
    printf("另一个字符串为:%s",b);
}

void copystr(char *from, char *to, int m)
{
    for (int i=1; i<m; i++) 
        from++;

    while (*from != '\0')
        *to++ = *from++;

    *to = '\0';
}

img


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


void copystr(string& a,int k,string &b)
{
    b = string(a.begin()+k - 1,a.end());
}
int main()
{
    string a,b;
    cin >> a;
    int k = 0;
    cin >> k;
    copystr(a,k,b);
    cout << b;
}

c++的string提供了这样的构造函数
用char* 换成strcpy(a+k-1,b);也行