# TCS NQT Caesar cipher Coding Question Solution

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

Problem Statement

The Caesar cipher is a type of substitution cipher in which each alphabet in the plaintext or message is shifted by a number of places down the alphabet.

For example, with a shift of 1, P would be replaced by Q, Q would become R, and so on.

To pass an encrypted message from one person to another, it is first necessary that both parties have the ‘key’ for the cipher, so that the sender may encrypt it and the receiver may decrypt it.

The key is the number of OFFSETs to shift the cipher alphabet. Key can have basic shifts from 1 to 25 positions as there are 26 total alphabets.

As we are designing custom Caesar Cipher, in addition to alphabets, we are considering numeric digits from 0 to 9. Digits can also be shifted by key places.

For example, if given plain text contains any digit with value 5 and key = 2, then 5 will be replaced by 7. “-” (Minus sign) will remain as it is. Key-value less than 0 should result in “INVALID INPUT”

**Example**

**Input**

All the Best

1

**Output**

Bmm uif Cftu

Write a program that will accept plaintext and key as input parameters and returns its ciphertext as output.

**Algorithm**

“`

3. if key < 0

then,

Print “INVALID INPUT”

exit

end if

4. key = key % 26

5. for every character in the string, do:

if the character has an ASCII value between 65 and 90 inclusive:

then,

Replace that character with:

(character + key – 65) % 26 + 65

else if the character has an ASCII value between 97 and 122 inclusive:

then,

Replace that character with:

(character + key – 97) % 26 + 97

else if the character has an ASCII value between 48 and 57 inclusive:

then,

Replace that character with:

(character + key – 48) % 10 + 48

end if

6. print string

TCS NQT Caesar Cipher Coding Problem Solution In C

``````#include <stdio.h>
int main()
{
char str;
scanf("%[^\n\t]s", str);
int key, i = 0;
scanf("%d", &key);
if (key < 0)
{
printf("INVALID INPUT");
return 0;
}
key = key % 26;
while(str[i] != '\0')
{
if (str[i] >= 65 && str[i] <= 90)
str[i] = (str[i] + key - 65) % 26 + 65;
else if (str[i] >= 97 && str[i] <= 122)
str[i] = (str[i] + key - 97) % 26 + 97;
else if (str[i] >= 48 && str[i] <= 57)
str[i] = (str[i] + key - 48) % 10 + 48;
i++;
}
printf("%s", str);
return 0;
}
``````

Program Solution in C++

``````#include <iostream>

using std::string;
using std::cout;
using std::cin;

int findNext (int value, int key)
{
if ( value >= 65 && value <= 90)
return (value - 65 + key) % 26 + 65;
if (value >= 97 && value <= 122)
return (value - 97 + key) % 26 + 97;
if (value >= 48 && value <= 57)
return (value - 48 + key) % 10 + 48;
return value;
}

int main() {
string text;
getline(cin, text);
int key;
cin >> key;
if ( key < 0 )
{
cout << "INVALID INPUT";
exit(0);
}
key = key % 26;
for ( size_t i = 0 ; i < text.size() ; i++ )
text.at(i) = findNext(text.at(i), key);
cout << text;
return 0;
}
``````

Program Solution In JAVA

``````import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int key = in.nextInt(), ch;
if (key < 0)
{
System.out.println("INVALID INPUT");
return;
}
key = key % 26;
for(int i = 0; i < str.length(); i++)
{
if (str.charAt(i) >= 65 && str.charAt(i) <= 90)
ch = (str.charAt(i) + key - 65) % 26 + 65;
else if (str.charAt(i) >= 97 && str.charAt(i) <= 122)
ch = (str.charAt(i) + key - 97) % 26 + 97;
else if (str.charAt(i) >= 48 && str.charAt(i) <= 57)
ch = (str.charAt(i) + key - 48) % 10 + 48;
else
continue;
str = str.substring(0, i) + Character.toString((char) ch) + str.substring( i + 1, str.length());
}
System.out.println(str);
}
}
``````

Program Solution In Java Using String Builder

``````import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
StringBuilder str = new StringBuilder(in.nextLine());
int key = in.nextInt(),ch;
if (key < 0)
{
System.out.println("INVALID INPUT");
return;
}
key = key % 26;
for(int i = 0; i < str.length(); i++)
{
if (str.charAt(i) >= 65 && str.charAt(i) <= 90)
ch = (str.charAt(i) + key - 65) % 26 + 65;
else if (str.charAt(i) >= 97 && str.charAt(i) <= 122)
ch = (str.charAt(i) + key - 97) % 26 + 97;
else if (str.charAt(i) >= 48 && str.charAt(i) <= 57)
ch = (str.charAt(i) + key - 48) % 10 + 48;
else
continue;
str.replace(i, i + 1, Character.toString((char) ch));
}
System.out.println(str);
}
}
``````

Program Solution In Python

``````if __name__ == "__main__":
str_ = input()
key_ = int(input())
if key_ < 0:
print("INVALID INPUT")
exit(0)
key_ = key_ % 26
i = 0
result = ''
while len(str_) > i:
if str_[i].isupper():
result += chr((ord(str_[i]) + key_ - 65) % 26 + 65)
elif str_[i].islower():
result += chr((ord(str_[i]) + key_ - 97) % 26 + 97)
elif str_[i].isdigit():
result += chr((ord(str_[i]) + key_ - 48) % 10 + 48)
i = i + 1

print(result)
``````