简体   繁体   中英

caesar cipher algorithm c++

i'm trying to implement Ceaser cipher in c++ language

#include <iostream>
#include <string>
#include <locale>

using namespace std;

int main()
{
    string word;


    getline(cin,word);

    for(int i=0; i<word.length();i++)
    {
        if(isalnum(word[i]))
        {
//shift by 3
            word[i]+= 3;
        }
    }
    cout << word ;
    return 0;
}

what i want is to limit the output also for only letter and number . for example if i want to shift z by 3 the output would be 'c' and not '}' as in my code .

Compilers are much better at handling the tedious details than humans, so in this case, I would write the code to show clearly what you intend, and then let the compiler figure out the numbers.

For example, if you want to shift a letter, don't you really just want to add 3 to the index of the letter in the range A to Z, and then mod by 26 -- the number of letters from A to Z? This is really what you want -- rotate around the circle of LETTERS from A to Z, of which there are 26, and not worry about ASCII values.

In that case, you can let the compiler figure it out for you:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int shift = 3;

    char* input = "Is it 90 with a ZERO?";
    printf("%s\n", input);

    int length = strlen(input);

    char* output = malloc(length + 1);
    output[length] = '\0';

    for (int i = 0; i < length; i++)
    {
        char c = input[i];
        if (c >= 'A' && c <= 'Z')
        {
            c = (((c - 'A') + shift) % 26) + 'A';
        }
        else if (c >= 'a' && c <= 'z')
        {
            c = (((c - 'a') + shift) % 26) + 'a';
        }
        else if (c >= '0' && c <= '9')
        {
            c = (((c - '0') + shift) % 10) + '0';
        }
        output[i] = c;
    }
    printf("%s\n", output);
}

Why would you want to take on that responsibility, if you are not worried about speed or memory footprint?

This should work.This assumes that there will be only lower case letters.

        word[i]+= 3;
        //at this point word[i] might have cross the ascii limit for lower case letters

ie may be word[i]>122 .Ascii range for lower case letters is 97-122 So we use mod to wrap it around. But now may be word[i]<97 which is again out of range so we add 97 to it.

        word[i]%=123;
        if(word[i]<97)word[i]+=97;

Example z

     word[i]+=3 makes word[i] as 125  //out of range
     word[i]%=123      //word[i]=3
     word[i]+=97          //word[i]=99=c

You have to make sure it does not go out of the valid range for ASCII letters. A way of doing this is to convert the input to lowercase, then make sure that when the shift is added, it does not exceed 122 ( z 's value in ASCII).

if (word[i] + SHIFT > 'z') {
    SHIFT -= 123 - word[i];
    word[i] = 'a';    // go back to the beginning
    word[i] += SHIFT; // add on the remaining amount
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM