Header Ads Widget

Program of CaesarCipher

 Program of CaesarCipher 

Have the function CaesarCipher (str, num) take the stx parameter and perform a Caesar Cipher shift on it using the num parameter as the shifting number. A Caesar Cipher works by shifting each letter in the string N places in the alphabet (in this case N will be num). Punctuation, spaces, and capitalization should remain intact. For example if the string is "Caesar Cipher and num is 2 the output should be "Ecguct Ekrjgt".

Examples:-

Input: "Hello" & num= 4

Output: Lipps


Input: "abc" & num=0

Output: abc




Program of CaesarCipher

The objective of the code is to  perform a Caesar Cipher shift on it using the num parameter as the shifting number .

C++ Code:-

#include<bits/stdc++.h>
using namespace std;
string CaesarCipher(string str,int num)
{
string result="";
for(int i=0;i<str.length();i++)
{
if(isupper(str[i]))
{
//Ascii of A is 65
result+=char(int(str[i]+num-65)%26+65);
}
else if(islower(str[i]))
{
// Ascii of a is 97
result+=char(int(str[i]+num-97)%26+97);
}
else
result+=str[i];
}
return result;
}
int main()
{
string str;
int num;
getline(cin,str);
cin>>num;
cout<<CaesarCipher(str,num);
return 0;
}

Output:

Caesar Cipher
2
Ecguct Ekrjgt

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-



  

Post a Comment

0 Comments