+ 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; }

16th Feb 2020, 11:58 AM
{ SorousH }
{ SorousH } - avatar
22 Antworten
+ 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?
16th Feb 2020, 2:59 PM
{ SorousH }
{ SorousH } - avatar
+ 4
~ swim ~ thank you very much. I didn't know this cpp feature and i was doing it on my c language knowledge.
16th Feb 2020, 3:21 PM
{ SorousH }
{ SorousH } - avatar
+ 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...
16th Feb 2020, 12:32 PM
Jayakrishna 🇮🇳
+ 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.
16th Feb 2020, 3:34 PM
Salman Nazeer
Salman Nazeer - avatar
+ 3
Keri Vaadaa Makkale Thank you very much
16th Feb 2020, 3:36 PM
{ SorousH }
{ SorousH } - avatar
+ 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
17th Feb 2020, 8:11 AM
SouthPoleHillbilly
SouthPoleHillbilly - avatar
+ 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
16th Feb 2020, 12:18 PM
Taste
Taste - avatar
+ 2
~ swim ~ The program does not include above 10 numbers...
16th Feb 2020, 2:07 PM
Jayakrishna 🇮🇳
+ 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..
16th Feb 2020, 2:21 PM
Jayakrishna 🇮🇳
+ 2
Swim, I have not programmed any check for 115 either and the code works fine for both C and C++
16th Feb 2020, 2:30 PM
Salman Nazeer
Salman Nazeer - avatar
+ 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
16th Feb 2020, 2:42 PM
Salman Nazeer
Salman Nazeer - avatar
+ 2
This code fails the 3rd test and I'm sure the third one involves 10
16th Feb 2020, 2:52 PM
Salman Nazeer
Salman Nazeer - avatar
+ 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().
16th Feb 2020, 3:17 PM
Salman Nazeer
Salman Nazeer - avatar
+ 2
It was a great learning experience.
16th Feb 2020, 3:34 PM
Salman Nazeer
Salman Nazeer - avatar
+ 2
Improve the algorithm of your code and then it will run all the tests
17th Feb 2020, 4:48 AM
Helios ❄
Helios ❄ - avatar
+ 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...
16th Feb 2020, 2:53 PM
Jayakrishna 🇮🇳
+ 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..
16th Feb 2020, 3:15 PM
Jayakrishna 🇮🇳
+ 1
You are welcome
16th Feb 2020, 3:41 PM
Salman Nazeer
Salman Nazeer - avatar
17th Feb 2020, 10:20 AM
հíʍɑղՏհմ
հíʍɑղՏհմ - avatar
+ 1
I mean, such publications will not be repeated
18th Feb 2020, 9:51 AM
معين البسارة
معين البسارة - avatar