-1

I've taken a code from here for a simple Caesar cipher, and I've modified it so that the user will define the cipher key. But the program crashes every time I tried to run it.

#include <stdio.h>

int main() 
{
    char array[100], cipher[100];
    int c=0, x=0, y=0;
    int z;
    printf("This Program will encrypt according to your needs\n");
    printf("Enter the cipher key\n");
    scanf("%d",&z);
    printf("Enter the sentence");
    while((c=getchar()) != '\n')
    {
        array[x++]=(char)c;
        cipher[y++]=(char)(c+z);
    }

    array[x]=0;
    cipher[y]=0;

    printf("%s\n",cipher);

    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
user3483503
  • 11
  • 1
  • 1
  • 5
  • 6
    if it crashes use a debugger, there was once a time when I debugged with log statements ... the hours that I could have saved by spending an hour learning how to use a debugger... I was dumb, you don't have to be. – Grady Player Apr 01 '14 at 04:22
  • what's the problem in your code? i tried it and it does not crash – WileTheCoyot Apr 01 '14 at 07:53

6 Answers6

5

It doesn't crash. The while loop ends instantly since the '\n' is in input buffer after scanf and this gets read first

avmohan
  • 1,820
  • 3
  • 20
  • 39
0

The scanf that reads in the key leaves a newline in the input buffer. Then when you call getchar for the first time, it returns \n, so the while loop is never entered.

You're not actually crashing, but just never getting an opportunity to enter in the string to encode.

You need to add an extra call to getchar after the scanf but before the loop to consume the newline:

scanf("%d",&z);
getchar();
dbush
  • 205,898
  • 23
  • 218
  • 273
0
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
    char p[20];
    int key,i,enc;
    clrscr();
    printf("Enter Plain text=");
    gets(p);
    printf("\n Enter Key=");
    scanf("%d",&key);

    for(i=0;i<strlen(p);i++)
    {
           p[i]=tolower(p[i]);
           enc=((p[i]-97)+key)%26;
           printf("%c",enc+97);
    }
}
  • Please provide the explanation as well. I can see that the user returning a 0 is kind of saying program is completed and exit the program and only difference the getch() method call does is to wait for user to key in any input if that is what you intended to explain please let us know how it makes the difference. – Jeet Jun 30 '16 at 17:09
-2
#include <stdio.h>
#include <conio.h>

void main()
    {
    int i, c;
    char str[100];

    printf("Enter the Text Message : ");
    gets(str);

    for (i = 0; i < strlen(str); i++)
        {
        switch (str[i])
            {
            case 'x':
                str[i] = 'a';
                continue;
            case 'y':
                str[i] = 'b';
                continue;
            case 'z':
                str[i] = 'c';
                continue;
            case 'X':
                str[i] = 'A';
                continue;
            case 'Y':
                str[i] = 'B';
                continue;
            case 'Z':
                str[i] = 'C';
                continue;
            }

        if (str[i] >= 'a' && str[i] < 'x')
            str[i] = str[i] + 3;
        else if (str[i] >= 'A' && str[i] < 'X')
            str[i] = str[i] + 3;
        }

    printf("Message After Encryption : \n");
    puts(str);
    for (i = 0; i < strlen(str); i++)
        {
        switch (str[i])
            {
            case 'a':
                str[i] = 'x';
                continue;
            case 'b':
                str[i] = 'y';
                continue;
            case 'c':
                str[i] = 'z';
                continue;
            case 'A':
                str[i] = 'X';
                continue;
            case 'B':
                str[i] = 'Y';
                continue;
            case 'C':
                str[i] = 'Z';
                continue;
            }

        if (str[i] >= 'd' && str[i] <= 'z')
            str[i] = str[i] - 3;
        else if (str[i] >= 'D' && str[i] < 'Z')
            str[i] = str[i] - 3;

        }
    printf("Message After Decryption : \n");
    puts(str);
    getch();
    }
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 2
    Please format your code and add a phrase to explain how this answers the question. – Lorenz Meyer Mar 03 '15 at 11:19
  • Please note that [`gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Feb 09 '19 at 17:26
-2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define MAXSIZE 1024

int main(int argc, char* argv[])
{
    char in[MAXSIZE];
    char en[MAXSIZE] = {0};

    //Input any Sting to encrypt in upper case or lower case
    //or also mixed-up in any case.
    read(STDIN_FILENO, in, MAXSIZE);
    encrypt(in, en);    
    printf("%s\n%s\n", in, en);
    bzero(in, strlen(in));
    decrypt(en, in);
    printf("%s\n%s\n", en, in);
    return 0;
}

int encrypt(char* input, char* cipher)
{
    int i;
    for(i = 0; i < strlen(input); i++)
    {
        if(input[i] >= 97 && input[i] <= 122)
        {
            cipher[i] = input[i]+23 <= 122 ? input[i] + 23 : input[i] - 3;
        }else if(input[i] >= 65 && input[i] <= 90)
        {
            cipher[i] = input[i]+23 <= 90 ? input[i] + 23 : input[i] - 3;
        }else
            cipher[i] = input[i];
    }
    return 0;
}

int decrypt(char* cipher, char* output)
{
    int i;
    for(i = 0; i < strlen(cipher); i++)
    {
        if(cipher[i] >= 97 && cipher[i] <= 122)
        {
            output[i] = cipher[i]-23 >= 97 ? cipher[i] - 23 : cipher[i] + 3;
        }else if(cipher[i] >= 65 && cipher[i] <= 90)
        {
            output[i] = cipher[i]-23 >= 65 ? cipher[i] - 23 : cipher[i] + 3;
        }else
            output[i] = cipher[i];
    }
    return 0;
}
-2

Here is a complete code in C for Caesar cipher

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int i,key;
    char plain[100],cipher[100];
    printf("Enter key:");
    scanf("%d",&key);
    key=key%26; // adjusting key
    fflush(stdin);
    printf("Enter text:");
    gets(plain);
    for(i=0;i<strlen(plain);i++)
    {
        if(isalpha(plain[i]))
        {
            if(islower(plain[i]))
                cipher[i]=(plain[i]+key-'a')%26+'a';
            else
                cipher[i]=(plain[i]+key-'A')%26+'A';
        }
        else
            cipher[i]=plain[i];
    }
    cipher[i]='\0';
    printf("After ciphering: %s",cipher);
    getch();
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sagun Shrestha
  • 1,188
  • 10
  • 23
  • Please note that [`gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Feb 09 '19 at 16:34