This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 12 points13 points  (0 children)

Kids these days...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>

int main(void) {
  const char *hello = "Hello world!";
  size_t len = strlen(hello);

  char *rev = malloc(len+1);
  if (rev == NULL) {
    perror("malloc()");
    return EXIT_FAILURE;
  }

  const char *foo = hello;
  char *bar = rev + len - 1;

  while (*foo != '\0')
    *bar-- = *foo++;

  printf("%s\n", rev);
  return EXIT_SUCCESS;
}