• C语言练习题:编写函数,该函数的功能是:移动字符串中的内容,移动的规则如下::把第1到第m个字符,平移到字符串的最后,把第m+1到最后的字符移到字符串的前部。例如,字符串原有的内容为ABCDEFGHI,m的值为4,移动后,字符串中的内容应该是EFGHIABCD


    下面是我自己写的一种方法,防止自己忘记

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

    #define M 100

    char *mov(char str1[],int m)
    {
      char *str2 = "";
      int i=0,j=0,n=0;

      n=strlen(str1);
      str2=(char *)malloc(M);//malloc (n);

      for (i=m;i<n;i++)
        str2[j++] = str1[i];

      for (i=0;i<m;i++)
        str2[j++] = str1[i];

      return str2;
    }

    int main ()
    {
      char str1[M] = "abcdefghijklmn";
      char *str2 = "";
      int m = 0, n =0;

      str2=(char *)malloc(M);
      n = strlen (str1);

      printf("Please input a num you want insert:");
      scanf("%d",&m);

      if ( m < n)   //this is a judge
      {
        str2=mov(str1,m);
        printf ("output is %s\n",str2);
      }

      else  //if the input "m" > n,exit
        printf("input error,exit\n");

      return 0;
    }

  • 相关阅读:
    [译]The Python Tutorial#4. More Control Flow Tools
    Python基础-函数参数
    Python进阶-继承中的MRO与super
    Python基础-类
    [译]The Python Tutorial#6. Modules
    Python基础-包与模块
    Python基础-类变量和实例变量
    Python进阶
    iphonex适配
    web前端安全
  • 原文地址:https://www.cnblogs.com/wanhl/p/2641726.html
Copyright © 2020-2023  润新知