-
Help with pointers!
I have this program that is suppose to give me the reversal of a message that i write, using pointers.
This is what i have, however, it seems there is a little problem with it because it gives me unknown symbols instead of the inverse words.
Can anyone tell me what´s wrong on it?
i added begin = c
but stills gives me an error..
Code:
#include "stdio.h"
int main()
{
int i=0,j;
char c,str[50],*begin;
begin=str;
printf("Enter a message : ");
while( (( c = getchar() ) != '\n') && (i < 49) )
{
begin = c;
begin++;
i++;
}
begin = '\0';
printf("Reversal is : \t");
for(j=i-1;j>=0;j--)
printf("%c",str[j]);
printf("\n");
}
-
Code:
for (j = strlen(str) - 1; j > -1; j--)
printf("%c";str[j]);
You only want the occupied part of the buffer, not the whole thing.
-
Code:
while( (( c = getchar() ) != '\n') && (i < 49) )
{
begin = str;
begin++;
i++;
}
Every time through your loop, it resets 'begin' to the start of the string. I don't think you want that line inside of your loop.
Quzah.
-
Code:
for(j=i-1;j>=0;j--)
printf("%c",str[j]);
Did you say you were supposed to use pointers?