반응형

Given a string, replace each its character by the next one in the English alphabet (z would be replaced by a).


Example


For inputString = "crazy", the output should be

alphabeticShift(inputString) = "dsbaz".


code>>


std::string alphabeticShift(std::string inputString) {

for (int i = 0; i<inputString.size(); i++)

{

inputString[i]++;

if (inputString[i] % ('z' + 1) == 0) inputString[i] = 'a';

}

return inputString;

}


다른 사람 코드>>


std::string alphabeticShift(std::string inputString) {

for (int i = 0; i<inputString.size(); i++)

inputString[i] = (inputString[i] + 1 - 'a') % 26 + 'a';

return inputString;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

30>circleOfNumbers  (0) 2017.07.06
29>chessBoardCellColor  (0) 2017.07.06
27>variableName  (0) 2017.07.06
26>evenDigitsOnly  (0) 2017.07.06
25>arrayReplace  (0) 2017.07.06

+ Recent posts