code.cpp 617 B

1234567891011121314151617181920
  1. using namespace std;
  2. /* Create a function that takes a string and replaces each
  3. * letter with its appropriate position in the alphabet.
  4. * "a" is 1, "b" is 2, "c" is 3, etc, etc.
  5. */
  6. string alphabetIndex(string str) {
  7. string result;
  8. for(int i = 0;i<str.size();i++){
  9. if(!isalpha(str[i])){continue;}
  10. // lowercase all values in string
  11. str[i] = tolower(str[i]);
  12. // calculate position of each letter
  13. int temp = str[i]-96;
  14. // add position of each letter to result, with a space
  15. result += to_string(temp)+ ' ';
  16. }
  17. // remove the last space at the end of result
  18. result.pop_back();
  19. return result;
  20. }