zoukankan      html  css  js  c++  java
  • 自行设计函数实现字符串复制

    【问题描述】
    字符串复制。
    输入一个字符串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] = '';//别忘记在末尾添加上结束标记符
    }
    
    
  • 相关阅读:
    洛谷 U141580 简化罗波切问题
    洛谷 U141578 维修电路
    洛谷 U140760 狭义公因子
    CF75C Modified GCD
    算法题-求解斐波那切数列的第N个数是几?
    算法题-求N的阶乘
    JAVA8新特性
    nginx启动脚本,手动编辑
    javah生成带有包名的头文件
    Matlab图像处理(03)-基本概念
  • 原文地址:https://www.cnblogs.com/yuzilan/p/10626134.html
Copyright © 2011-2022 走看看