Is there in C language a way to do something like:
/* it's a demo code. I know that it doesn't work. */
char foo[] = "abc";
char* p = &foo[0];
int len = 3;
while(len) {
printf("%c", *p);
p--;
len--;
}
and get cba output?
My question is: there any simple way to do this? maybe using arithmetic pointers. I know that I can write an function-like:
/* Note: I haven't tested the this code. But I believe that works. */
char *strrev(char input[]) {
if (input == NULL) return NULL;
int len = strlen(input) - 1;
char * rev = malloc(len+1);
if (rev == NULL) return NULL;
for (; len != 0; len--) *rev++ = input[len];
if (len == 0) {
*rev++= '\0';
return rev;
} else {
free(rev);
return NULL;
}
}
But I’m looking for more simple way, I need write a function that starts the comparisons from half string.
Example usage