0

Where is the problem in my code?

I'm doing an exercise, where you input 2 words and the program checks if you can make the 2nd word from the letters of the 1st one. If you can, it outputs "NO", if you can then "YES". Now, I sent it in the website where the exercise is hosted for it to be checked, but it only got through 11/12 tests, so something is wrong, but I can't notice what. Here's the code: https://code.sololearn.com/cY9g96MBf0m0/#cpp

20th Oct 2017, 3:20 PM
Mr.E
Mr.E - avatar
11 Answers
+ 3
They may be testing your assumption that someone will enter 2 words. When I enter 1 word and omit the other one, your program reports YES. How do I make word2 from the letters of word1, if word1 is blank? I'm saying "might" (as in maybe) because the rules could read: two words are always provided.
20th Oct 2017, 3:50 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
It is for the task, because in this language you're allowed to go out of bounds. It just assumes you want to do that, and happily provides whatever is in memory past the string. Using this input: ABCDEF G Insert this at line 15 to see what I mean: cout << word1[i] << " : [" << word2[i] << "]" << endl; Notice how when B-F are tested on the left, the right side has unintended values. You could fix this by converting both words to uppercase (no need to test), scanning each word separately (by individual length) or something you think is better.
20th Oct 2017, 9:20 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Any way you could convert this to a CodePlayground link?
20th Oct 2017, 3:13 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
If I enter either of these, I get YES (quick guess). word1: abc <blank> word2: <blank> abc (might be an input validation test)
20th Oct 2017, 3:25 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Also, do they care about UPPERCASE vs lowercase?
20th Oct 2017, 4:07 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
line 12: If the words are different lengths, you're testing past the end of one of them...
20th Oct 2017, 7:27 PM
Kirk Schafer
Kirk Schafer - avatar
20th Oct 2017, 3:19 PM
Mr.E
Mr.E - avatar
0
@Kirk Schafer I don't understand what you mean.
20th Oct 2017, 3:40 PM
Mr.E
Mr.E - avatar
0
@Kirk Schafer I updated the code with your suggestions (I think). Even though in CodeBlocks the program didn't accept blank as an answer as it does in sololearn. I took notice of the task saying that the input must be uppercase latin letters and there must be at least 1 and not more than 250 characters, but it still says 11/12 tests passed ;| check out my updated code.
20th Oct 2017, 6:38 PM
Mr.E
Mr.E - avatar
0
Is that bad? I assumed nothing would happen since nothing != lowercase and this would be more compact.
20th Oct 2017, 8:49 PM
Mr.E
Mr.E - avatar
0
if your input has expected values set out, the code asker is expecting you to test for those conditions. in my opinion. why would they specify otherwise?
21st Oct 2017, 12:23 AM
Dill Pickle
Dill Pickle - avatar