ETTUBRUTE -Editorial

PROBLEM LINK:

Practice

Author: Kunal Demla
Editorialist: Kunal Demla

DIFFICULTY:

CAKEWALK

PREREQUISITES:

String

PROBLEM:

Given a String and a number, shift all characters by the number.

QUICK EXPLANATION:

Traverse the string replace all alphabets with Char-Num.

EXPLANATION:

Traverse the String and replace all Aplhabets with char- num and add 26 if it goes below ‘A’ or ‘a’.

ALTERNATE EXPLANATION:

Substract ‘a’ or ‘A’ to make it out of 26, add (26-num) and mod 26.

SOLUTIONS:

Setter's Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n,m,x,y,i,j,k;
    string s;
    getline(cin,s);
    getline(cin,s);
    cin>>n;
    n=n%26;
    for(auto ch:s){
        if((ch<='z'&&ch>='a')){
            char temp=ch-n;
            if(temp<'a'){
                temp=temp+26;
            }
            cout<<temp;
        }
        else if(ch<='Z'&&ch>='A'){
            char temp=ch-n;
            if(temp<'A'){
                temp=temp+26;
            }
            cout<<temp;
        }
        else{
            cout<<ch;
        }
    }    
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
// cout<<t<<endl;
// ${2:is Single Test case?}cin>>t;
cin>>t;
int n=t;
while(t--)
{
    //cout<<"Case #"<<n-t<<": ";
    solve();
    cout<<"\n";
}

cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
return 0;
}