Take the 2-minute tour ×

I'm getting a blank output in my console screen when I try to print the string value in a reverse order. If I use for loop for printing the String value is printing, but when I simply print using %s, it is not printing? Why?

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
    char name1[10],name2[10];
    int i,len,j;


    printf("\nEnter the string that u want to get reversed:");



    printf("\nThe reversed string is:");
share|improve this question

closed as too localized by Jonathan Leffler, WhozCraig, David Segonds, Fahim Parkar, Bo Persson Nov 24 '12 at 8:07


A possible duplicate question: How do you reverse a string in place in C or C++? –  Jonathan Leffler Nov 24 '12 at 3:33
add comment

3 Answers

up vote 3 down vote accepted

You're starting your reversed string by placing the nullchar terminator from the original as the first char. Thus as far as printf() is concerned, it is an empty string (zero length).

Apart from that, this needs serious code review. This is honestly not the place for it, as a review of proper argument would likely rewrite the entire thing (except maybe the beginning scanf() and ending printf(). I strongly advise you run this step-by-step in a debugger to see what is happening as your code runs.

share|improve this answer
And the first of the individual characters written is an ASCII NUL '\0' which doesn't appear with a visible representation. –  Jonathan Leffler Nov 24 '12 at 3:23
@JonathanLeffler If i did the math on the counters on this, I think the result is both leading and trailing with an ASCI NUL. I've had way too much turkey to be that detailed at this point in the evening =P –  WhozCraig Nov 24 '12 at 3:27
For code review: codereview.stackexchange.com –  tjameson Nov 24 '12 at 3:30
add comment

some notes for you:

  • format your code properly before posting
  • you used too many "printf"s to see variable values. that means u don't know how to DEBUG your code properly please google for HOW TO DEBUG CODE + "IDE name you are using" (fe. HOW TO DEBUG CODE VISUAL STUDIO)
  • always add commments to your code
  • before posting something like that pls GOOGLE first! The "reverse a string" problem is a typical "Inroduction to Programming" assignment and it's not hard to find a sample code for that. at the beginning code study is more useful than just randomly typing some bulshit code
  • you should give your variables more meaningful names f.e. in your case 'src' and 'target' are much better choices than name1 and name2 of course in such a short code it's not important but get used to it asap
  • try to solve such problems first on paper for visualization before starting coding
  • since you try to deal with dynamic string content by reading input from user, it's a better idea to use dynamic allocation i.e malloc()/free() than using fix arrays.
share|improve this answer
add comment

You can always just use the strrev function from the standard string.h library

share|improve this answer
Which standard is that? (Hint: it isn't standard in ISO/IEC 9899:2011 — the C standard; nor is it standard in POSIX.) –  Jonathan Leffler Nov 24 '12 at 3:19
You're right. Sorry I read it from a bad resource. –  Dillon Benson Nov 24 '12 at 3:25
It's likely some other library, and it is likely declared in <string.h> when you're using that library, so it might seem like it's 'standard'. That said, die.net does not recognize it, and Is strrev() function not available on Linux suggests that it is not glibc, the GNU C library. –  Jonathan Leffler Nov 24 '12 at 3:27
add comment
