This blog is under construction

Sunday 7 July 2013

C program to convert plain text to cipher text and vice versa

Write a C program to convert plain text to cipher text and vice versa.


  #include <stdio.h>
  #include <string.h>
  int main() {
        char plainText[100], cipherText[100], val;
        int i = 0, key;

        /* get the plain text from the user */
        printf("Enter the plain text(all caps):");
        fgets(plainText, 100, stdin);

        /* key to convert plain text to cipher text */
        printf("Enter the key to create cipher text(1 - 5):");
        scanf("%d", &key);
        plainText[strlen(plainText) - 1] = '\0';

        /* converting plain text to cipher text */
        while (plainText[i] != '\0') {
                if ((plainText[i] + key) > 'Z') {
                        val = (plainText[i] + key) - 'Z';
                        cipherText[i] = 'A' + val - 1;
                } else {
                        cipherText[i] = plainText[i] + key;
                }
                i++;
        }

        cipherText[i] = '\0';

        printf("Resultant Cipher Text: %s\n", cipherText);
        i = 0;

        /* converting cipher text to plain text */
        printf("Plain Text:");
        while (cipherText[i] != '\0') {
                if ((cipherText[i] - key) < 'A') {
                        printf("%c", 'Z' + 1 - ('A' - (cipherText[i] - key)));
                } else {
                        printf("%c", cipherText[i] - key);
                }
                i++;
        }
        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the plain text(all caps):XYZABD
  Enter the key to create cipher text(1 - 5):5
  Resultant Cipher Text: CDEFGI
  Plain Text:XYZABD





See Also:

5 comments:


  1. Thanks for your extraordinary blog. Your idea for this was so brilliant. This would provide people with an excellent tally resource from someone who has experienced such issues. You would be coming at the subject from a different angle and people would appreciate your honesty and frankness. Good luck for your next blog!
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  2. Thanks 😊
    You can also find Programming exercise in c++ here.
    https://myustaadg.com/category/programs/

    ReplyDelete