Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 5
what's wrong with my solution?
l wrote this solution for No Numeral (Code Coach) and just 2 test failed (test 3,4)! what's wrong with this code?! #include <iostream> #include <cstring> using namespace std; int main() { string s; while (cin>>s){ int n=s.length(); char c[n]; for(int i=0;i<n;i++){ c[i]=s[i]; switch(c[i]){ case '2': cout<<"two"; break; case '3': cout<<"three"; break; case '4': cout<<"four"; break; case '5': cout<<"five"; break; case '6': cout<<"six"; break; case '7': cout<<"seven"; break; case '8': cout<<"eight"; break; case '9': cout<<"nine"; break; case '0': cout<<"zero"; break; } if(c[i]=='1'){ if(s[i+1]=='0') { cout<<"ten"; i++; } else cout<<"one"; } if(c[i]>96 && c[i]<123) cout<<c[i]; } cout<<" "; } return 0; }
22 Answers
+ 5
Jayakrishna ~ swim ~ Keri Vaadaa Makkale Taste
Thank you very much for helping. my code doesn't consider any difference between numbers <10 and >10 and it convert all numerals to word form.
I think it's better to first check weather the string isdigit() or not then if it was true cast it to integer by atoi and then by using an if and a switch find the right form and cout it. right?
or i try to improve the previous code?
+ 4
~ swim ~ thank you very much.
I didn't know this cpp feature and i was doing it on my c language knowledge.
+ 3
It works fine but you need to print 0-10 numbers in words, and everything else is as it is to be printed.
printing extra space..
So may be that is the case...
+ 3
https://code.sololearn.com/c7rbgjytYQbG/?ref=app
Here i have solved the problem. I added an else and a default statement in your switch.
+ 3
Keri Vaadaa Makkale Thank you very much
+ 3
Make empty output string
Try
Parseint the words
Switch numbur to word if int
Add new word to output string
Catch
Add word to output string
Don't see 10 as a combination of 1 and 0 but as 10
+ 2
the question is
```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.```
but in yours >10 is still a numeral, your code convert any number without any check
+ 2
~ swim ~
The program does not include above 10 numbers...
+ 2
But I coded like if current charecter is 0 to 9 or 10 (1+0), then replaced with word.
And every thing else prints as it is..
+ 2
Swim, I have not programmed any check for 115 either and the code works fine for both C and C++
+ 2
This is your code with my changes. It now passes 5/6 test cases. But can't figure out the rest.
P.s: it will be better if you attach the code rather than paste it here.
https://code.sololearn.com/c7rbgjytYQbG/?ref=app
+ 2
This code fails the 3rd test and I'm sure the third one involves 10
+ 2
My solution involves for loops and if else if. I have used a c string. I did not use any functions other than basic i/o functions and strlen().
+ 2
It was a great learning experience.
+ 2
Improve the algorithm of your code and then it will run all the tests
+ 1
Keri Vaadaa Makkale
Yes. That print zero extra..
Edit:
Let him try and try until gets it, since it a good way to practice.
My opinion don't give code to anybody until they ask ....
Give only step by step suggestions...
+ 1
{ SorousH }
I specified my way already. I did it in C, with a for loop with switch case only. No any predefined methods.
If it still not passed all cases, then guess is due to space including for after last word also... So if it is true, then use getline method or use dowhile loop..
+ 1
You are welcome
+ 1
I mean, such publications will not be repeated