fork download
  1. #include <iostream>
  2. #include <string>
  3. #include<cstdio>
  4. using std::cin;
  5. using std::string;
  6. using std::cout;
  7. using std::endl;
  8.  
  9. int main()
  10. {
  11. int k,n;
  12. string text;
  13. cin>>n;
  14. cin>>text;
  15. cin>>k;
  16. k=k%26; //taking MOD 26 because it is the size of English alphabet.
  17. char ans;
  18. int extra,letter;
  19. for (int i = 0; i <n; i++)
  20. { //cout<<text[i]<<" : ";
  21. // if(text[i] != '\0') //check if the character is null
  22. {
  23. if(isalpha(text[i])) //check if the character is alphabet
  24. {
  25. int d=(int)text[i] ; //obtain the ascii value
  26.  
  27. int t=d+k; //obtain the encrypted value
  28. //if(t>122)
  29. { //cout<<t<<","<<d<<" ... ";
  30. if(d>96) //if the encrypted value of character is greater than 'a', small caps
  31. {
  32. if(t>122) { //if value is greater than 'z' ASCII is 122
  33. extra=t-122;
  34. letter=97 +(extra-1); //97 is ASCII of 'a'
  35.  
  36. }//string ans= string()+char(letter);
  37. else
  38. letter=char(t);
  39. }
  40. else // Upper caps
  41. { if(t>90) { //90 is ASCII of 'Z'
  42. extra=t-90;
  43. letter=65 +(extra-1); //65 is ASCII of 'A'
  44. }
  45. else
  46. letter=char(t);
  47. }
  48. ans=letter;
  49. //cout<<char(letter);//cout<<ans;
  50. }
  51. //else
  52. //string ans= string()+char(t); //print the encrypted character
  53. cout<<char(ans); //cout<<ans;
  54. }
  55. else //if the character is not string, then print it just like that
  56. {
  57. cout<<text[i];
  58. } //cout<<endl;
  59. }
  60. }
  61. }
Success #stdin #stdout 0s 3460KB
stdin
11
middle-Outz
2
stdout
okffng-Qwvb