欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言实现字符串循环左移和右移

程序员文章站 2022-04-30 22:49:26
...

C语言实现循环左移和右移

这个没有什么好说的,直接上代码:

#include <stdio.h>

//写一个函数 实现功能:输入字符串,向右循环或者逆时针移动N字节,再输出这个字符串
/*
    //比如:左移2字节
    //左边不变,右边变
    p[0] = s[4];  //LEN - (n - i)%LEN i = 0
    p[1] = s[5];  //LEN - (n - i)%LEN i = 1
    p[2] = s[0];  //LEN - (n - i)%LEN i = 2
    p[3] = s[1];  //LEN - (n - i)%LEN i = 3
    p[4] = s[2];  //
    p[5] = s[3];  //
    //右边不变,左边变
    p[2] = s[0];  //a   len - (n - i)%LEN i = 0
    p[3] = s[1];  //b                     i = 1
    p[4] = s[2];  //c                     i = 2
    p[5] = s[3];  //d                     i = 3
    p[0] = s[4];  //e  len -                    i = 4
    p[1] = s[5];  //f                     i = 5
    
    p[(n + i)%LEN] = src[i];
    
    //比如:左移5字节
    
    p[0] = s[1];  //len - (n - 0)%LEN   i = 0
    p[1] = s[2];  //len - (n - 1)%LEN   i = 1
    p[2] = s[3];  //len - (n - 2)%LEN   i = 2
    p[3] = s[4];  //len - (n - 3)%LEN   i = 3
    p[4] = s[5];  //len - (n - 4)%LEN   i = 4
    p[5] = s[0];  //len - (n - 5)%LEN   i = 5
    
    
    
*/
//左边不变,右边变
void ClockWise1(const char* src,char* result,int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    n = n%LEN;
    for(i = 0;i < LEN;i++)
    {
        result[i] = src[(LEN - (n - i))%LEN];
    }
    result[i] = '\0';   
} 
//右边不变,左边变
void ClockWise2(const char* src,char* result,int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    for(i = 0;i < LEN;i++)
    {
        result[(n + i)%LEN] = src[i];
    }  
    result[i] = '\0';   
}

void ClockWise3(const char* src,char* result,int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    n = n%LEN;
    for(i = 0;i < n;i++)//也可以用strcy代替,下面函数就是
    {
        result[i] = src[LEN - n + i];
    } 
    for(i = 0;i < LEN - n;i++)
    {
        result[n + i] = src[i];
    }
    result[LEN] = '\0';   
}  
void ClockWise4(const char* src,char* result,int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    n = n%LEN;
    strcpy(result,src + LEN - n);
    strcpy(result + n,src);
    result[LEN] = '\0';   
}
//写一个函数 实现功能:输入字符串,向左循环或者顺时针移动N字节,再输出这个字符串
/*
    //比如:左移移2字节
    //左边不变,右边变
    p[0] = s[2];  //(n + i)%LEN
    p[1] = s[3];  
    p[2] = s[4];  
    p[3] = s[5];  
    p[4] = s[0];  
    p[5] = s[1];  
    //右边不变,左边变
    p[5] = s[0];  //b (LEN - n + i)%LEN   i = 0
    p[0] = s[1];  //c                     i = 1
    p[1] = s[2];  //d                     i = 2
    p[2] = s[3];  //e                     i = 3
    p[3] = s[4];  //f               i = 4
    p[4] = s[5];  //a                     i = 5
    
    //比如:左移5字节
    
    
*/   
void AntiClockWise1(const char* src,char* result,unsigned int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    for(i = 0;i < LEN;i++)
    {
        result[i] = src[(n + i)%LEN]; 
    }
    result[i] = '\0';
}  
void AntiClockWise2(const char* src,char* result,unsigned int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    n = n%LEN;
    for(i = 0;i < LEN;i++)
    {
        result[(LEN - n + i)%LEN] = src[i];
    }
    result[i] = '\0';
} 
void AntiClockWise3(const char* src,char* result,unsigned int n)
{
    const unsigned int LEN = strlen(src);
    int i;
    n = n%LEN;
    for(i = 0;i < n;i++)
    {
        result[LEN - n + i] = src[i];
    }
    for(i = 0;i < LEN - n;i++)
    {
        result[i] = src[n + i];
    } 
    result[LEN] = '\0'; 
}    
int main()
{
    char result[255] = {0};
    printf("ClockWise1 start\n");
    ClockWise1("abcdef",result,3);
    printf("%s\n",result);
    ClockWise1("abcdef",result,5);
    printf("%s\n",result);
    ClockWise1("abcdef",result,8);
    printf("%s\n",result);
    ClockWise1("abcdef",result,12);
    printf("%s\n",result);   
    printf("ClockWise1 end\n\n");
    
    printf("ClockWise2 start\n");
    ClockWise2("abcdef",result,3);
    printf("%s\n",result);
    ClockWise2("abcdef",result,5);
    printf("%s\n",result);
    ClockWise2("abcdef",result,8);
    printf("%s\n",result);
    ClockWise2("abcdef",result,12);
    printf("%s\n",result);   
    printf("ClockWise2 end\n\n");
    
    printf("ClockWise3 start\n");
    ClockWise3("abcdef",result,3);
    printf("%s\n",result);
    ClockWise3("abcdef",result,5);
    printf("%s\n",result);
    ClockWise3("abcdef",result,8);
    printf("%s\n",result);
    ClockWise3("abcdef",result,12);
    printf("%s\n",result);   
    printf("ClockWise3 end\n\n");
    
    printf("ClockWise4 start\n");
    ClockWise4("abcdef",result,3);
    printf("%s\n",result);
    ClockWise4("abcdef",result,5);
    printf("%s\n",result);
    ClockWise4("abcdef",result,8);
    printf("%s\n",result);
    ClockWise4("abcdef",result,12);
    printf("%s\n",result);   
    printf("ClockWise4 end\n\n");
    
    printf("AntiClockWise1 start\n");
    AntiClockWise1("abcdef",result,3);
    printf("%s\n",result);
    AntiClockWise1("abcdef",result,5);
    printf("%s\n",result);
    AntiClockWise1("abcdef",result,8);
    printf("%s\n",result);
    AntiClockWise1("abcdef",result,12);
    printf("%s\n",result);   
    printf("AntiClockWise1 end\n\n");   
    
    printf("AntiClockWise2 start\n");
    AntiClockWise2("abcdef",result,3);
    printf("%s\n",result);
    AntiClockWise2("abcdef",result,5);
    printf("%s\n",result);
    AntiClockWise2("abcdef",result,8);
    printf("%s\n",result);
    AntiClockWise2("abcdef",result,12);
    printf("%s\n",result);   
    printf("AntiClockWise2 end\n\n"); 
    
    printf("AntiClockWise3 start\n");
    AntiClockWise3("abcdef",result,3);
    printf("%s\n",result);
    AntiClockWise3("abcdef",result,5);
    printf("%s\n",result);
    AntiClockWise3("abcdef",result,8);
    printf("%s\n",result);
    AntiClockWise3("abcdef",result,12);
    printf("%s\n",result);   
    printf("AntiClockWise3 end\n\n");
    return 0;
}

C语言实现字符串循环左移和右移

没打印完,你们可以自己调试看下

参考资料《狄泰软件C语言进阶》