Given a string, convert it into its number form.
- A or a -> 1
- B or b -> 2
- C or c -> 3 . . .
- Z or z -> 26
- space -> $
Example:
Input: s ="AMbuj verma"
Output: 11322110$22518131
Approach
C++
#include <bits/stdc++.h>using namespace std;string conversion(string s){string res = "";for (int i = 0; i < s.size(); i++){if (s[i] >= 'a' && s[i] <= 'z')res += to_string(s[i] - 'a' + 1);else if (s[i] >= 'A' && s[i] <= 'Z'){res += to_string(s[i] - 'A' + 1);}elseres += '$';}return res;}int main(){string s = "AMbuj verma";cout << conversion(s) << "\n";return 0;}
No comments:
Post a Comment