C编程实现字符串移位操作

  • 基本原理

  1. 先malloc一个n个字节大小的buff, char * buf =(char*) malloc(n);其中n为字符串求要左移的位数;

  2. 先将要求左移的字符串str的前n个字节的数据拷贝至我们刚刚创建的buf中;

  3. 利用memmove函数将str的第n个字节的数据移动至str的开始处;

  4. 在利用memcpy或strncpy将buf中的数据拷贝至str的str[strlen(str) - num]地址处。

  • 图示

 

  • C编程实现代码

/*********************************************************************************
 *      Copyright:  (C) 2020 ysn
 *                  All rights reserved.
 *
 *       Filename:  str.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(15/08/20)
 *         Author:  tianjincheng <473892093@qq.com>
 *      ChangeLog:  1, Release initial version on "15/08/20 12:36:35"
 *                 
 ********************************************************************************/

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

char *str_leftshift(char *str, int num)
{
    char * res = (char *)malloc(num);
    if (NULL == res )
        return NULL;

    if (NULL == str || num > strlen(str))
        return NULL;


    memcpy(res, str, num);
    memmove(str, &str[num], strlen(str) - num);
    memcpy(&str[strlen(str) - num], res, num);
    str[strlen(str)] = '\0';

    free(res);
    return str;
}

int main()
{
    char  dst[200];
    int  n = 0;
    memset(dst, 0, sizeof(dst));
    scanf("%s", dst);
    scanf("%d", &n);
    char * res = str_leftshift(dst, n);
    if (res != NULL)
        printf("%s\n", res);

}
  • 运行结果

  •  注意

  1. ​​​​​​​千万不要放了free我们malloc产生的buf。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
我可以回答这个问题。位密码是一种简单的加密方式,它将明文中的每个字符按照一定的规则进行位,从而得到密文。例如,将明文中的每个字符向右动3个位置,即A变成D,B变成E,以此类推。加密过程可以用C语言实现如下: ```c #include <stdio.h> #include <string.h> void encrypt(char *plaintext, int shift) { int len = strlen(plaintext); for (int i = 0; i < len; i++) { if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { plaintext[i] = 'A' + (plaintext[i] - 'A' + shift) % 26; } else if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { plaintext[i] = 'a' + (plaintext[i] - 'a' + shift) % 26; } } } void decrypt(char *ciphertext, int shift) { int len = strlen(ciphertext); for (int i = 0; i < len; i++) { if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') { ciphertext[i] = 'A' + (ciphertext[i] - 'A' - shift + 26) % 26; } else if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') { ciphertext[i] = 'a' + (ciphertext[i] - 'a' - shift + 26) % 26; } } } int main() { char plaintext[100], ciphertext[100]; int shift; printf("请输入明文:"); scanf("%s", plaintext); printf("请输入位数:"); scanf("%d", &shift); strcpy(ciphertext, plaintext); encrypt(ciphertext, shift); printf("加密后的密文为:%s\n", ciphertext); decrypt(ciphertext, shift); printf("解密后的明文为:%s\n", ciphertext); return 0; } ``` 在这个程序中,我们定义了两个函数encrypt和decrypt,分别用于加密和解密。这两个函数都接受两个参数,一个是要加密或解密的字符串,另一个是位数。加密过程中,我们遍历明文中的每个字符,如果是大写字母,则将其动shift个位置,取模26后再加上'A',得到加密后的字符;如果是小写字母,则同样处理,只是加上的是'a'。解密过程与加密过程类似,只是位数变成了负数,加上的是26-shift。 在主函数中,我们首先输入明文和位数,然后调用encrypt函数进行加密,将加密后的密文存储在ciphertext数组中,最后调用decrypt函数进行解密,将解密后的明文输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoCheng'Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值