1

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;

    clrscr();

    printf("\nEnter the string that u want to get reversed:");
    scanf("%s",&name1);
    for(i=0;name1[i]!='\0';i++);
    len=i;

    j=i;

    for(i=0;i<=len;i++)
    {
        name2[i]=name1[j];
        j--;
    }

    printf("\nThe reversed string is:");
    printf("%s",name2);
    getch();
}
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Praveen
  • 121
  • 1
  • 2
  • 8
  • A possible duplicate question: [How do you reverse a string in place in C or C++?](http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) – Jonathan Leffler Nov 24 '12 at 03:33

3 Answers3

3

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.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 2
    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 03: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 03:27
  • 1
    For code review: codereview.stackexchange.com – beatgammit Nov 24 '12 at 03:30
1

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.
A.B.
  • 2,374
  • 3
  • 24
  • 40
0

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

Dillon Benson
  • 4,280
  • 4
  • 23
  • 25
  • 3
    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 03:19
  • You're right. Sorry I read it from a bad resource. – Dillon Benson Nov 24 '12 at 03:25
  • It's likely some other library, and it is likely declared in `` when you're using that library, so it might seem like it's 'standard'. That said, [die.net](http://linux.die.net/man/) does not recognize it, and [Is `strrev()` function not available on Linux](http://stackoverflow.com/questions/8534274/is-strrev-function-not-available-in-linux) suggests that it is not `glibc`, the GNU C library. – Jonathan Leffler Nov 24 '12 at 03:27