打印

[提问] 哪位会写这个C++程序???

哪位会写这个C++程序???

有这样一种加密规则:按26个英文字母顺序将原来的字符串的每个字母后移3位,如字符串"computer"加密后为"frptxwu",编写一个程序,由用户输入一串字符,通过加密输出结果.

TOP

很简单啊~~~
定义一个类,包括一个字符数组成员,再定义一个成员函数用于将数组成员每个字符都后移三位就可以了~~~
牵着你的手,一步一步向前走~~~
没有在电脑上试,随意写的。
#include<iostream.h>
main()
{
    int i=0,j;
    char a[50],b[50];
    cin>>a;
    while(a!='\0')
    {
       b=a+3;
        i++;
     }
     cout<<b;
}

TOP

不知道这样行不行:

char* cpt(const char *key)
{
        char *aa = (char*)malloc(strlen(key)+1);
        int pos;
        while(key++)
        {
                pos = *key - 0x40;
                if(pos<24||pos>0x20&&pos<0x38)
                {
                        *aa=*key + 3;
                }else if(pos>0x18&&pos<0x1B||pos>0x37)
                {
                        *aa=*key - 0x18;
                }
                aa++;
        }
        *aa='\0';
        return aa;
}
i,j????????????????lou shang de ??

TOP

回复 4# 的帖子

看不懂嘛~~~我才学C++4天就碰到书上有这一题,都不知道该怎么写.
根据上面两位修改得来,仅限于小写字母

#include <iostream.h>
#include <string.h>

void main()
{
        int flag = 3;//移动位数
    char str[50];

    cin >> str;
   
        for(int i=0; i<strlen(str); i++)
        {
                str = (str + flag - 'a' )%26 + 'a';
        }

    cout << str << endl;
}

TOP

是不 x变a y变b z变c
#include <iostream.h>
#include <string.h>

void main()
{
        int flag = 3;//移动位数
    char str[50];

    cin >> str;
   
        for(int i=0; i<strlen(str); i++)    //strlen(str)什么意思??
        {
                str = (str + flag - 'a' )%26 + 'a';      //这一句都看不懂
        }

    cout << str << endl;
}



TOP

回复 9# 的帖子

strlen(str)是求str的长度
str = (str + flag - 'a' )%26 + 'a';就是把str的每个字母后移flag位

Processed in 0.053450 second(s), 5 queries, Gzip enabled