• + 0 comments

    c++ solution this solution uses vector to store the new position of charecters after shiftting it by k element then the corrosponding indexs are retrive to build the cipher text

    {
        string cipher_text="";
        vector<char>alphabet(26);
        for (int i = 0; i < 26; ++i) {
            alphabet[i] = 'a' + (i + k) % 26;
        }
        
        for (int i=0; i<s.size();i++)
        {   
            char c=s[i];
            bool f=false;
    // edge case of uppercase we have to convert it to lowercase first then after finding the corresponding //chsr make it uppercase again
            if (c >='A'&& c<='Z')
            {
                c = tolower(s[i]);
                f=true;
            }
            
            int idx=c-'a';
            if(idx>=0 && idx<=25){
                if(f)
                {
                      cipher_text+=toupper(alphabet[idx]);
                }
                else {
                      cipher_text+=alphabet[idx];
                }
              
            }
            else
            {
                cipher_text+=c;
            }
        
        }
        return cipher_text;
    
    }