- 2

string strcmp() function

https://code.sololearn.com/c0a10A12a6a6 8.14 Write and test the strcmp() function. is my code correct? and here what N is returning is the ASCII value right?

29th Apr 2021, 8:10 AM
Ramisa Fariha
Ramisa Fariha - avatar
7 Answers
+ 5
The strcmp() function is not correct. The for-loop will run only once as you're returning a value from the function from inside the loop. Furthur, if '*s1 != *s2' in the first place, then the for-loop will not be executed and your program will return nothing (for integers, nothing=garbage value) due to the absence of a return statement. Also, the return value of the strcmp() function is NOT the ASCII value. In your code as well as the standard library, the strcmp() function returns - a negative value if str1 < str2 - 0 if str1 == str2 - a positive value if str1 > str2 where str1 and str2 are strings passed as arguments. Fix: In the strcmp() function, the first condition of the loop (`*s1 == 0`) is correct, so you don't need to change that. The second condition is wrong, but the value returned from the condition is correct. The fix to the problem would be to remove the 'else' condition inside the for-loop, and move the return statement just outside the for-loop. That will fix the function.
29th Apr 2021, 8:55 AM
XXX
XXX - avatar
29th Apr 2021, 8:55 AM
XXX
XXX - avatar
+ 1
The strcmp() function is not correct.
29th Apr 2021, 7:50 PM
RENE OPOKU
RENE OPOKU - avatar
+ 1
The string function strcmp() is used to compare strings...it returns the highest valued string according to the ASCII value ..ex: strcmp(a,z); Returns z becoz the ASCII value of z is greater than a.
30th Apr 2021, 3:36 AM
Abhishek_singh
Abhishek_singh - avatar
0
thank you so much @ XXX
29th Apr 2021, 9:35 AM
Ramisa Fariha
Ramisa Fariha - avatar
0
@Martin Taylor I keep the codes I make for the Discuss section private because in most cases, they don't make sense outside the context of the question. And after all, there is really no use in upvoting codes which are made as an answer to a question. It's been a long time since I used the browser version of SoloLearn. So just a small question - can you see who upvoted your answer on the web version (as far as I can remember, you can't)? Also, I saw your profile and found that a long time back, you actually commented on codes. This makes me wonder, what made you move from the SoloLearn app to the browser version?
29th Apr 2021, 4:28 PM
XXX
XXX - avatar
0
It takes two string arguments and compare between them on the basis of ASCI value and returns the difference between them.
29th Apr 2021, 7:24 PM
Anmol Pandey
Anmol Pandey - avatar