+ 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
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
+ 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));
}
+ 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
+ 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.
+ 1
Slick
It is better to use fgets() to get input it appeaneds '\0' (NULL) character to buffer passed to fgets() function.
DHANANJAY
- 1
Slick ?