-3

in the below program I need to reverse string without using library function. But the output after copying prints some garbage value. Can anyone kindly help me in figuring it out? Please find the code and output

6
  • First thing you need to do is define a character limit and stick with it, otherwise you can expect a segmentation fault. Also, its more helpful to post code as text on the screen instead of a picture of half a computer program. Commented Feb 28, 2016 at 4:39
  • 1
    1. #include <stdio.h> at the top of the file. 2. Fix compiler errors. 3. If further questions, please copy/paste code as text, not images. Commented Feb 28, 2016 at 4:40
  • Possible duplicate of Garbage being printed when using strcpy Commented Feb 28, 2016 at 5:02
  • How the heck did this compile?? Even with the missing #include, there should have been compile errors with str1[i] and str2[j]!! Commented Feb 28, 2016 at 5:58
  • You must fix any compiler messages before worrying about the output of yourprogram. If you don't see any messages then you need to figure out where to look in your IDE to see the messages. Commented Feb 28, 2016 at 6:23

2 Answers 2

2

str1 and str2 must be char arrays large enough to hold the strings. For your simplistic example, char str1[128], str2[128]; should do.

You must initialize j to the length of str1 before the loop and null terminate str2 with str2[j--] = '\0'; before the loop:

for (j = 0; str1[j] != '\0'; j++) {
    continue;
}
str2[j--] = '\0';
for (i = 0; str1[i] != '\0'; i++, j--) {
    str2[j] = str1[i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

but strlen requires string.h library, OP wants to do without using it
@ch3rub7: you are correct, I removed the <string.h> dependency.
terminating str2 with null produces desired result. if str2 is not terminated with null, the output is like "ayvid garbage value divya"(as in output screen in pic one). I need to know how come after garbage value it prints the original string again at the end. Can anyone please clarify?
@divya: I'm not sure what code you are referring to: the code in the picture is broken, it should not compile. If you do not null terminate your string, the code invokes undefined behavior, leading to erratic behavior. A precise explanation for observed behavior in such circumstances is not very useful.
0

str2 should be assigned with null character '\0' after the string reversal in for loop.

for(j=strlen(str1);str1[i]!='\0';i++,j--)
        str2[j-1]=str1[i];
str2[i]='\0';

1 Comment

if str2 is not terminated with null, the output is like "ayvid garbage value divya"(as in output screen in pic one). I need to know how come after garbage value it prints the original string again at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.