C++函数问题6

[复制链接]
查看11 | 回复0 | 2009-1-30 06:28:38 | 显示全部楼层 |阅读模式
编一加密的程序,将输入的一行字符串中的所有字母加密,非字母字符不变。方法是:将每一个字符加一序数(序数须为1到5之间的整数),该序数称为密钥。如果加上序数后字符值大于‘Z\'或‘z\',则转换为A~E(或a~e)之一,即‘A\'->‘F’,‘a’->‘f’,‘B’->‘G’....‘Y’->‘D’,‘Z’->‘E’。函数形式为void encrypt(char s[ ],int n); 希望详细点,谢谢。

                                                                                                #include <iostream>#include <assert.h>using namespace std;void encrypt(char s[],int n){        assert(s != NULL);                for(int i=0; i<strlen(s); i++)        {                if(s >= \'a\' && s <= \'z\')                {                        s = (s-\'a\'+n)%26+\'a\';                }                if(s >= \'A\' && s <= \'Z\')                {                        s = (s-\'A\'+n)%26+\'A\';                }        }}void main(){        char str[100];        cout<<"please input a string:";        cin>>str;        int n;        cout<<"please input a number:";        cin>>n;        encrypt(str, n);        cout<<"加密后:"<<str<<endl;}                                       
提问者对答案的评价:
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行