有一行文字,具体长度和内容自行约定,设计两个函数:(1)count函数:统计并输出其中英文字母、数字以及其他字符的个数。(2)code函数:译密码,将字符串中的字母按下述规律转换:将字母A变成

有一行文字,具体长度和内容自行约定,设计两个函数:

(1)count函数:统计并输出其中英文字母、数字以及其他字符的个数。

(2)code函数:译密码,将字符串中的字母按下述规律转换:将字母A变成字母E,a变成e,即变成其后的第4个字母,W变成A,X变成B,Y变成C,Z变成D,非字母字符不变。

主函数中先输入该行文字,然后分别调用count函数和code函数,主函数最后输出原文和密文。

参考运行截图:

这里主要难点是w x y z的地方无法直接用+=4解决

源代码:

#include <stdio.h>

#include <string.h>

void  tongji(char a[50]) {

int zimu = 0,shuzi = 0, qita = 0;

int len = strlen(a);

for (int i = 0; i < len; i++) {

    if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) {

       zimu++;

    }

    else if (a[i] >= '0' &a[i] <= '9') {

       shuzi++;

    }

    else {

       qita++;

    }

}

printf("字母的个数:%d\n数字的个数:%d\n其他字符的个数:%d\n", zimu, shuzi, qita);

}

void code(char a[50]) {

int len = strlen(a);

printf("原文:");

puts(a);

for (int i = 0; i < len; i++) {

    if (a[i] >= 'a' && a[i] <= 'z')

       a[i] = ((a[i] - 'a' + 4) % 26) + 'a';    

    if (a[i] >= 'A' && a[i] <= 'Z')

       a[i] = ((a[i] - 'A' + 4) % 26) + 'A';

}

printf("密文:");

puts(a);

}

int main() {

char arr[50];

gets(arr);

tongji(arr);

code(arr);

return 0;

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值