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

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

#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;
}

转载于:https://www.cnblogs.com/wanhl/archive/2012/08/16/2641726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值