strcpy和memcpy的使用,实现字符串的循环右移,

来源:互联网 发布:假面骑士drive知乎 编辑:程序博客网 时间:2024/05/01 20:57
#include <stdio.h>#include <string.h>#define MAX_LEN 16void rotate_right(char *pstr, int steps);int main(void){char arr[] = "abcdefghi";int step;printf("Before rotate right move string is:%s\n", arr);printf("Enter your step:");scanf("%d", &step);rotate_right(arr, step);printf("After rotate right move string is:%s\n", arr);return 0;}/*void rotate_right(char *pstr, int steps){int n = strlen(pstr) - steps;char temp[MAX_LEN];strcpy(temp, pstr + n);strcpy(temp + steps, pstr);*( temp + strlen(pstr)) = '\0';strcpy(pstr, temp);}*/void rotate_right(char *pstr, int steps){int n = strlen(pstr) - steps;char temp[MAX_LEN];memcpy(temp, pstr + n, steps);memcpy(pstr + steps, pstr, n);memcpy(pstr, temp, steps);}

0 0
原创粉丝点击