Ad
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    char* reverse_string(const char* word) {
        if (!word) {
            return NULL;
        }
    
        size_t len = strlen(word);
        char* res = (char*)malloc(len + 1);
        if (!res) {
            return NULL;
        }
    
        for (size_t i = 0; i < len; ++i) {
            res[i] = word[len - 1 - i];
        }
        res[len] = '\0';
    
        return res;
    }
    • #include <stdlib.h>
    • #include <string.h>
    • char* reverse_string(const char* word) {
    • if (!word) {
    • return NULL;
    • }
    • char *reverse_string(const char *word)
    • {
    • if (!word)
    • size_t len = strlen(word);
    • char* res = (char*)malloc(len + 1);
    • if (!res) {
    • return NULL;
    • char *res = malloc(strlen(word) + 1);
    • for(int i = strlen(word) ; i>=0 ; res[i] = word[strlen(word) - (--i) - 1]);
    • res[strlen(word)] = '\0';
    • return res;
    • }
    • }
    • for (size_t i = 0; i < len; ++i) {
    • res[i] = word[len - 1 - i];
    • }
    • res[len] = '\0';
    • return res;
    • }