Write a C program to convert plain text to cipher text and vice versa.
#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
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:
- c program to print "Hello World"
- c program to print "Hello World" without using semicolon
- c program to swap two numbers
- c program to convert Celsius to Fahrenheit
- c program to convert Fahrenheit to Celsius
- c program to convert time in Hours:Minutes:Seconds to seconds
- c program to find the area of triangle given three sides
- c program to calculate Simple Interest
- c program to print multiplication table from 1 to n
- c program to check whether the given year is leap year or not
- c program to find the size of basic data types
- c program to find the area and circumference of a circle
- c program to find the roots of a quadratic equation
- c program to add two numbers
- c program to swap two numbers without using temporary variable
- c program to check whether the given number N is divisible by M
- c program to add two fractions
- c program to add two numbers without third variable
- c program to convert improper fraction to mixed fraction
- c program to convert centimeters to inches and feet
- c program to convert feet to inches
- c program to convert days into months and days
- c program to convert kilometers to miles, meters and feet
- c program to convert kilometer per hour to meter per second
- c program to convert kilograms to pounds and grams
- c program to convert minutes into hours
- c program to convert time in seconds to hours, minutes and seconds
- c program to convert hours, minutes to seconds
- c program to convert meter to feet and centimeters
- c program to convert plain to cipher text and cipher to plain text
- c program to convert pounds to kilograms
- c program to convert radians to degrees
- c program to convert year to roman equivalent
- c program to calculate compound interest
- c program to calculate power of a number
- c program to calculate factorial
- c program to find the area & perimeter of a circle, triangle, square and rectangle
- c program to calculate age in years, months and days
- c program to find absolute value of a given number
- c program to calculate age from date of birth
- c program to calculate body mass index
- c program to calculate distance between two points
- c program to calculate Greatest Common Divisor(GCD)
- c program to calculate Highest Common Factor(HCF)
- c program to calculate Least Common Multiple(LCM)
- c program to simplify the given fraction using GCF
- c program to calculate Gross and net salary of an employee
- c program to calculate Grades
- c program to calculate GPA
- c program to calculate income tax
- c program to calculate modulus
- c program to implement digital clock
- c program to calculate profit and loss
- c program to calculate percentage
- c program to calculate PI, resistance and power
- c program to calculate square root and square of a number
- c program to calculate volume of a cylinder and sphere
- c program to calculate weighted arithmetic mean
- c program to calculate x power y
- c program to check whether a date is valid or not
- c program to convert days into years, months and weeks
- c program to print date and sleep for a given time
- c program to clear screen and authenticate user
- c program to calculate generic root of any number
- c program to find largest digit of a number
- c program to generate multiplication table for the given number
- c program to convert decimal to binary and count the number of ones and zeros
- c program to round a number
- c program to guess a random number
- c program to implement ceaser cipher
- c program to swap nibble in a byte
- c program to find slope
- c program to calculate nCr and nPr
- c program to print even, odd and prime factors of a given number
- c program to print the four digit numbers whose sum of squares of first half and second half of the number equals the same number
- c program to check whether a given number is magic number or not
- c program to calculate correlation coefficient
ReplyDeleteI am feeling happy to read this. You gave nice info to me. Please update more.
Hibernate Training in Chennai
Spring Hibernate Training in Chennai
Spring Training in Chennai
Spring framework Certification
Spring framework Training
Spring Hibernate Training
Struts Training in Chennai
Wordpress Training in Chennai
Great blog! You are providing a lot of valid information. This is really helpful. Do share more, looking forward to learn more from you.
ReplyDeleteBlue Prism Training in Chennai
Blue Prism Training Chennai
AWS Training in Chennai
DevOps certification in Chennai
VMware Training in Chennai
Azure Training in Chennai
Cloud Computing Training in Chennai
Blue Prism Training in Anna Nagar
I have to appreciate you for your great work which you had done in your blog.i want you to add more like this.
ReplyDeleteJAVA Training in Chennai
JAVA Training in Tambaram
IOS Training in Chennai
Big data training in chennai
Python Training in Chennai
Selenium Training in Chennai
JAVA Training in Velachery
ReplyDeleteThanks 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
Thanks 😊
ReplyDeleteYou can also find Programming exercise in c++ here.
https://myustaadg.com/category/programs/