*RESOLVED* It prints the null char too. Why?
PLEASE IGNORE. SORRY! FIGURED IT OUT! FEELING LIKE AN IDIOT.
Just practicing. My StrReverse works like a charm but the only complaint is that it prints the null char as well on the screen. Why?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* StrReverse(char*);
int main(void)
{
char* str;
char* ptr;
char cArray[] = "Hello, how are you?";
str=cArray;
ptr=0;
ptr = StrReverse(str);
if (ptr) printf("%s\n",ptr);
}
char* StrReverse(char* str)
{
char* temp;
int i =0,j =0;
i=strlen(str);
temp = (char*)malloc((i+1)* sizeof(char));
if (temp)
{
for(j=0;j<i;j++)
*(temp+j) = *(str+i-j-1);
*(temp+j)='\0';
}
return temp;
}