Saturday, August 4, 2018

C PROGRAM TO CREATE A ENCRYPTED FORMAT FOR THE GIVEN MESSAGE

STATEMENT;

Kate wants to encrypt the message M which is to be sent to his business partner Bharat. So he shifts every alphabet by X positions in forward direction and he adds Y to every number in the message.
Given a string value M of the message and the values of X and Y, the program must print the encrypted message E.

- All the alphabets will be in lower case.
- Spaces and special characters in the message M should be reproduced as such in the encrypted message E.

PROGRAM:

#include <stdio.h>

int main()
{

   char msg[100];

    fgets(msg,100,stdin);

    char *ptr = msg;

    int X,Y;

    scanf("%d%d",&X,&Y);

    char current;

    while(*ptr != '\r' && *ptr != '\n' && *ptr != '\0')
    {

        current = *ptr;

        if(isalpha(current))
        {

            char modified = current+X;

            if(modified - 'z' > 0)
            {

                char toprint = 'a'+ (modified-'z')-1;

                printf("%c",toprint);
            }
            else
            {

                printf("%c",modified);
            }

        }

        else if(isdigit(current))
        {

            int currentdigit = current - '0';

            printf("%d",currentdigit+Y);
        }
        else
        {

            printf("%c",current);
        }

        ptr++;
    }
}


Input:
credit 1 lakh
3
0

Output:
fuhglw 1 odnk

No comments:

Post a Comment