logo
down
shadow

Caesar cipher for numbers in C


Caesar cipher for numbers in C

By : 8MoLa
Date : September 24 2020, 01:00 AM
To fix this issue Here is the general algorithm:
write the number in ASCII into a buffer, iterate over the characters in the buffer, for each character, if it's a digit, add 3, if the resulting ASCII code is bigger than '9', subtract 10
code :


Share : facebook icon twitter icon
Encrypting with large numbers, using a Caesar Cipher

Encrypting with large numbers, using a Caesar Cipher


By : Balakrishnan Kaliyam
Date : March 29 2020, 07:55 AM
Any of those help In Java, the result of modulo is the same sign as the dividend. So when you compute c - '!' - 1000 you will get a negative value and after the modulo it will still be negative. When you add '!', you will have computed a values less than '!', which will be invisible or underflow for a char.
code :
static char decrypt(char c) {
    char d = '~' - '!' + 1;
    int x = (c - '!' - 1000) % d;
    if (x < 0) x += d;
    return (char)('!' + x);
}
How to account for spaces and numbers in Caesar's cipher?

How to account for spaces and numbers in Caesar's cipher?


By : tscurrie
Date : March 29 2020, 07:55 AM
it fixes the issue I am trying to encrypt a message using CAESAR'S CIPHER and the encryption also works correctly. But now I would like to know how to account for spaces and numbers in the string to be encrypted. , Like this
code :
<?php
    //the text to be encrypted
    $plain_text='ABC';
     echo $plain_text."<br>";
    //letters of alphabet array
    $alphabet=array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 
    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1','2','3','4','5','6','7','8','9','0',' ');
    //positions of the letters in alphabet
    $flip=array_flip($alphabet);
    $mod = count( $alphabet );

    //plaintext array
    $plain_text=str_split($plain_text);
    $n=count($plain_text);
    $encrypted_text='';
    for ($i=0; $i<$n; $i++){
        $n = strtoupper( $n );
        //encryption
        $encrypted_text.=$alphabet[($flip[$plain_text[$i]]+2) % $mod];
    }

    echo $encrypted_text;
?>
   $alphabet = array_unique($alphabet);
Is there another way to check whether it's an integer in Caesar Cipher other than a for loop? CS50 pset2 Caesar Cipher

Is there another way to check whether it's an integer in Caesar Cipher other than a for loop? CS50 pset2 Caesar Cipher


By : Ruben Pennise
Date : March 29 2020, 07:55 AM
I wish this help you Because return exits the function, there’s no need to use an else after an if branch containing a return – if the if path is taken, nothing after the if keeps running.
code :
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    for (int i = 0; i < strlen(argv[1]); i++)
    {
        if (!isdigit(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }

        printf("Sucess\n");
        printf("%s\n", argv[1]);
    }
}
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    for (int i = 0; i < strlen(argv[1]); i++)
    {
        if (!isdigit(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    printf("Success\n");
    printf("%s\n", argv[1]);
}
Caesar's cipher for numbers in C

Caesar's cipher for numbers in C


By : user3477586
Date : March 29 2020, 07:55 AM
wish of those help This solution works for all your test cases (positive and negative numbers), for numbers greater than 100000 and for zero as well:
code :
#include "stdio.h"
#include "stdlib.h"

const int SHIFT = 3;

int main() {
    int number, abs_number, is_negative, operation, result, decimal_place;

    do {
        result = 0;
        decimal_place = 1;

        printf("Enter the number: ");
        scanf("%d", &number);

        // remove the sign before entering the cipher logic...
        abs_number = abs(number);
        // ...but do remember it so we can add it back later
        is_negative = number < 0;

        // from least to most significant decimal place
        do {
            // cipher the digit and accumulate it in the result
            result += (((abs_number % 10) + SHIFT) % 10) * decimal_place;
            // remove the consumed digit from the input variable
            abs_number /= 10;
            // move to the next decimal place
            decimal_place *= 10;
        } while (abs_number > 0);

        printf("Result is: %s%d\n", is_negative ? "-" : "", result);

        printf("Press 1 to continue or 2 for exit.");
        scanf("%d", &operation);

    } while (operation == 1);  // repeat if user requested another cipher

    return 0;
}
Caesar Cipher Negative Numbers Issue

Caesar Cipher Negative Numbers Issue


By : Karthik_nodedev
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You could just add the 26 to ch when ch < 'a' or ch <'A'. Consider below example.
code :
    if(ch < 'a')
     {
      ch  = ch + 26;
     } 
int encrypt(int character, int shift) {
    int ch = character;
    int negShift = shift;

    if (ch >= 'a' && ch <= 'z') {
        ch = ch + shift;
        if (ch > 'z') {
            ch = ch - 'z' + 'a' - 1;
        }

         else if(ch < 'a')
         {
            ch  = ch + 26; //Here
         } 

    } else if (ch >= 'A' && ch <= 'Z') {
        ch = ch + shift;
        if (ch > 'Z') {
            ch = ch - 'Z' + 'A' - 1;
        }

        else if(ch < 'A')
        {
            ch = ch + 26; // Here 
        }

    }

    return ch;
}
Related Posts Related Posts :
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • Passing a generic argument in a C function
  • Restoring stdout after redirecting it via freopen()
  • Tuples "cannot be marshalled in a foreign call" when trying to export a Haskell function to C
  • Dereferencing double pointer to pass to recursive function
  • Why is the output different in these two scenarios
  • what will happen if we dont use free() for allocated memory
  • counting words from user input
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk