+ 4

Why do i get garbage values?

Made a string reversing code and every time i feed it a longer string, it spits out some weird characters then prints the message. What am i doing wrong here? Also, why doesn't this run in SL? https://code.sololearn.com/cTviGMwcS0qe/?ref=app

16th Mar 2021, 10:24 AM
Slick
Slick - avatar
10 odpowiedzi
+ 4
I honestly can't get it to accept here. But when i run it through termux or an app, I get those extra values sometimes. Just wondering what i did wrong. Coder Kitten I've given up on trying to run it in SL, Just trying to run it through gcc on my phone. But that was totally my problem, I did not add a nullchar when I broke on one. Seems if you don't, even if you have the correct index, random values will still be printed sometimes. I'll just say undefined behavior! Thank you Jayakrishna🇮🇳 and Coder Kitten
16th Mar 2021, 11:04 AM
Slick
Slick - avatar
+ 2
Your for loop initializations are not in effect.. try this void print_rev(char string[]) { int len = length(string) - 1; for (int i = len; i >= 0; i--) putchar(*(string+i)); }
16th Mar 2021, 10:38 AM
Jayakrishna 🇮🇳
+ 2
They were, i ran these through an external compiler. I also changed to yours and another version of the for loop with the same issue. Here's input and output https://www.sololearn.com/post/988469/?ref=app see those values that are printed first in the reverse string? What even are those? EDIT: Jayakrishna🇮🇳 gave it a shot, switched all calls to "length_of" and still got a timeout here. It's really no worries, I appreciate your help big time
16th Mar 2021, 10:46 AM
Slick
Slick - avatar
+ 1
Am getting the correct answer for that input.. Depending your logic for taking input, you must enter \n. Otherwise It won't accept here in Sololearn.. edit: @Slick you may entering \0 with input ? edit: Slick i guess, in other compilers that length(string) may refers to predefined function length(), not your defined. so it returning 25 i think.. not sure.. try with change in name.
16th Mar 2021, 10:53 AM
Jayakrishna 🇮🇳
+ 1
Slick It is better to use fgets() to get input it appeaneds '\0' (NULL) character to buffer passed to fgets() function. DHANANJAY
17th Mar 2021, 2:43 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
- 1
30th Mar 2021, 8:53 AM
Michael
Michael - avatar