#PROBLEM LINK:
Author: Ayush Nagal
#DIFFICULTY:
EASY
#PREREQUISITES:
String
#PROBLEM:
Given a positive integer n, a string s and an integer k. We need to print the new string by rotating the characters of s by k.
#EXPLANATION:
First of all, we need to extract the ASCII value of each character in the string s. After that, we add k to the ASCII value and take modulus by 26 so that ASCII value doesn’t exceed the range of alphabets.
for(int i=0;i<n;i++)
{
if(s[i]>='a' && s[i]<='z')
{
int t=s[i]-'a';
t+=k;
t=t%26;
s[i]=t+'a';
}
if(s[i]>='A' && s[i]<='Z')
{
int t=s[i]-'A';
t+=k;
t=t%26;
s[i]=t+'A';
}
}
As it is clear from the solution, whenever the ith character of s is an alphabet, we shift it by k.
#AUTHOR’S SOLUTION:
Author’s solution can be found here.