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