-5

For Caesar cipher encryption, I have this code. This program uses text written by the user. But I want this to be read from a text file and run.

#include<stdio.h>
#include <conio.h>
#include<string.h>

void main()
{
  int key,i;
  char [30];
  clrscr();
  printf("\n enter plain text : ");
  gets(data);
  printf("\ enter key value : ");
  scanf("%d",&key);
  {
    for (i=o;i<strlen(data);i++) {
      if (data[i]==' ') {}
      else
      {
        if (data[i]>='x')
        {
          data[i]=data[i]-26;
        }
        data[i]=data[i]+key;
      }
    }
  } 
  printf("your cipher text is : %s",data);
  getch();
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Please do yourself a favor and format your code. As it stands here it is unreadable. – Jabberwocky Apr 04 '17 at 13:33
  • 3
    Just read it from the file then? – Fredrik Apr 04 '17 at 13:33
  • 1
    Don't use `gets`. It is dangerous, have been deprecated since the C99 standard, and removed completely from the latest C11 standard. Depending on platform use the standard C [`fgets`](http://en.cppreference.com/w/c/io/fgets) function or the POSIX [`getline`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) function instead. – Some programmer dude Apr 04 '17 at 13:33
  • ...and you should stop using TurboC. – Jabberwocky Apr 04 '17 at 13:35
  • 5
    As for your problem of reading from a file, [any good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) should tell you how to do that. – Some programmer dude Apr 04 '17 at 13:35

1 Answers1

2

Did you just copy and paste this code from somewhere?

As everyone else has already pointed out, there are some pretty big problems with your code, so I'll try and touch on the ones they haven't mentioned throughout. To start though, you should declare main as an int. The return value tells you if the program exited correctly, it's not just convention.

Here is your original code, formatted. I removed conio.h because it's been deprecated for years and moreover you don't need it here. All it was doing was clearing the screen for you, which you can do with System("CLS");, although here is a great article that's been shared many times here on SO that explains why you should refrain from using it.

#include<stdio.h>
#include<string.h>

int main()
{
  int key,i;

  // original: char [30];
  // I'm guess you copied and pasted wrong?
  char plainText[30];

  printf("\n enter plain text : ");
  gets(data);
  printf("\nenter key value : ");
  scanf("%d",&key);

  // You had brackets here, essentially creating an unnecessary
  // block of code.
  for(i=o;i<strlen(data);i++)
  {
    // Where did the "data" variable come from? You haven't declared it
    // and you're trying to get its length in this for loop
    if (data[i]==' ')
    {
      // ?
    }
  else
    {
      if (data[i]>='x')
      {
          // Instead of this approach I would go with a modulus operation
          // If you're not familiar with modular arithmetic I really recommend you look
          // it up. It's absolutely essential in cryptography. It's central to both
          // symmetric and asymmetric key cryptography.
          data[i]=data[i]-26;
      }

    // This is the heart of the Caesar cipher right here. This is where you're actually
    // shifting the characters, so it's a literal Caesar cipher  
    data[i]=data[i]+key;
    }
  }

  // Again, you haven't declared "data" so you can't call it. I'm guessing you didn't
  // try to compile this program because it is teeming with errors GCC, Clang, and VC++
  // would have caught immediately
  printf("your cipher text is : %s",data);
  getch();

  // Remember to make your "main" function return an <code>int</code>. This value is
  // very important, especially when your program gets called by another program because
  // it's how your program communicates that it ran successfully, or something went
  // wrong. In that case you could set a specific error code to know exactly
  // what went wrong and were

  // Example:
  int x = 1;
  if (x == 1)
      exit(4);

  // This program would now exit with a status of 4, which you would then know was due
  // to this function. This was a contrived example, but hopefully you get the gist
  return 0;
}
dw_112233
  • 21
  • 4