I am trying to do something as simple as printing the reverse of a string . EXAMPLE :
Hello World! This is me
Needed O/P:
me is This World! Hello
My code goes something like this:
#include<stdio.h>
#include<string.h>
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%s",&arr[i]);
for(i=0;i<size;i++)
{
printf("%s\n",&arr[size-1-i]); //overwritten words
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is %s\n",(char *)revarr);
}
I except arr[0] , arr[1] etc to be separate entities but on printing and storing them they seem to be overlapping like this : i/p:
Hello World
o/p:
World
HellWorld
the reversed sentence is WorlHell@#$
I can't seem to figure out what is wrong! Thanks in advance!
EDIT : On printing
printf(&arr[0]);
printf(&arr[1]);
I get :
HellWorld
World
What I expected it to print is
Hello
World