+ 1
No Numerals
You write a phrase and include a lot of number characters (0-9), but you decide that for numbers 10 and under you would rather write the word out instead. Can you go in and edit your phrase to write out the name of each number instead of using the numeral? Task: Take a phrase and replace any instances of an integer from 0-10 and replace it with the English word that corresponds to that integer. Input Format: A string of the phrase in its original form (lowercase). Output Format: A string of the updated phrase that has changed the numerals to words. Sample Input: i need 2 pumpkins and 3 apples Sample Output: i need two pumpkins and three apples My solution to the above problem is in comment section
3 Antworten
+ 2
When s[0] is '1' you need to check the next character for '0' in case it is part of "10". Then be sure to avoid translating numbers larger than 10 (e.g., "100"). And for added challenge be certain that the numerals are not embedded within other text (e.g., "1st").
+ 1
#include <iostream>
using namespace std;
void replace(string s) {
if(s.length() == 0) {
return;
}
if(s[0] == '0') {
cout<<"zero";
replace(s.substr(1));
}
else if(s[0] == '1') {
cout<<"one";
replace(s.substr(1));
}
else if(s[0] == '2') {
cout<<"two";
replace(s.substr(1));
}
else if(s[0] == '3') {
cout<<"three";
replace(s.substr(1));
}
else if(s[0] == '4') {
cout<<"four";
replace(s.substr(1));
}
else if(s[0] == '5') {
cout<<"five";
replace(s.substr(1));
}
else if(s[0] == '6') {
cout<<"six";
replace(s.substr(1));
}
else if(s[0] == '7') {
cout<<"seven";
replace(s.substr(1));
}
else if(s[0] == '8') {
cout<<"eight";
replace(s.substr(1));
}
else if(s[0] == '9') {
cout<<"nine";
replace(s.substr(1));
}
else {
cout<<s[0];
replace(s.substr(1));
0
}
}
int main() {
string s;
cin>>s;
replace(s);
return 0;
}
This code is working properly in the c++ code but not in the solution to the above problem. Kindly tell me the solution to the above problem.