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();
}
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 endingprintf(). I strongly advise you run this step-by-step in a debugger to see what is happening as your code runs.