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

C语言实现左旋字符串

程序员文章站 2024-01-06 21:30:22
-------------------------------------------------------------------------------------- &n...
--------------------------------------------------------------------------------------

      例如:字符串:AABCD     左旋一个字符为:ABCDA

                              左旋两个字符为:BCDAA

------------------------------------------------------------------------------------------

 

C语言代码:

 

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

# define MAX 20

void Contra_solem(char *str, int move)
{
    char *pstart = NULL;
    char *pend = NULL;
    char *src = NULL;
    int i = 0;

    pstart = str;
    pend = (str + strlen(str));
    src = str;

    for (i = 0; i < move; i++)
    {
        *pend++ = *pstart++;
     }

    while (pstart <= pend)
    {
        *src = *pstart;
        pstart++;
        src++;
     }
}

int main()
{
    char str[MAX] = "abcdefgh";
    int move = 0;                        

    printf("源字符串为:%s\n",str);

    printf("输入想要左旋字符的个数:");
    scanf("%d", &move);                

    while (move > strlen(str))
    {
    printf("@@@输入太大,请重新输入!@@@\n");
    scanf("%d",&move);
    }

    Contra_solem(str,move);

    printf("左旋%d个字符后的字符串为:%s\n",move,str);

    system("pause");
    return 0;
}

 

 

上一篇:

下一篇: