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

    xiaoxiao2025-04-10  15

    #include <stdio.h> #include <string.h> #define MAX_LEN 16 void 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); }
    转载请注明原文地址: https://ju.6miu.com/read-1297911.html
    最新回复(0)