C Program to Encode a String and Display Encoded String
Problem Statement :
Read a five-letter word into the computer, then encode the word on a letter-by-letter basis by subtracting 30 from the numerical value that is used to represent each letter. Thus if the ASCII character set is being used, the letter a (which is represented by the value 97) would become a C (represented by the value 67), etc. Write out the encoded version of the word. Test the program with the following words: white, roses, Japan, zebra.
Program : To Encode Entered String and Display Encoded String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<stdio.h> #include<conio.h> char* encode(char* str) { int i = 0; while (str[i] != '\0') { str[i] = str[i] - 30; // Subtract 30 From Charcter i++; } return (str); } void main() { char *str; printf("\nEnter the String to be Encode : "); gets(str); str = encode(str); printf("\nEncoded String : %s", str); getch(); } |
Output :
1 2 3 4 5 6 7 8 |
Enter the String to be Encode : white Encoded String : YJKVG Enter the String to be Encode : roses Encoded String : TQUGU Enter the String to be Encode : Japan Encoded String : ,CRCP Enter the String to be Encode : zebra Encoded String : GDTC |