Saturday, December 5, 2020

Introduction to Programming in C Slot 1: Question 2 Solution

 

Slot 1: Question 2

Due on 2020-12-05, 11:00 IST
You are given a string (of at most 100 characters) consisting of
lowercase characters, terminated with a # symbol. The string is the
"encrypted" form of an original string as follows - each character in
the original string has been shifted by a fixed integer n (where 1<= n
<= 25).

Assuming that a is the 0th character in the alphabet, ..., and z is
the 25th character of the alphabet, in the encrypted version, we have: 

a becomes the nth character in the alphabet
b becomes the (n+1)%26th character in the alphabet,
...

and so on.

n is not known to you. You only know that the first character
represents 't'. From this information, you have to output the
original string.



Your last recorded submission was on 2020-12-05, 10:53 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
int main() {
4
    char s[100];
5
    scanf("%s", s);
6
    int diff = 't' - s[0];
7
    int i = 0;
8
    while(s[i] != '#') {
9
        s[i] += diff % 26;
10
        printf("%c", s[i]);
11
        i++;
12
    }
13
    return 0;
14
}
15

No comments:

Post a Comment