DEV Community

Discussion on: Reverse a string: awful answers only

Collapse
krussellsmith profile image
K. Russell Smith • Edited on
#include <stdio.h>
#include <stdlib.h>
int len(const char*str)
{
    int result=0;
GETATIT:
    if (*(str+result++)=='\0') goto END;
    goto GETATIT;
END:
    return result;
}
char*reversed(const char* str)
{
    #define reset i=0
    char*result=(char*)malloc(sizeof(char)*len(str));
    int reset;
YOUCANDOIT:
    if ((*(result+len(str)-1-i)=*(str+i))=='\0')
        goto ONEMORESTEP;
    ++i;
    goto YOUCANDOIT;
ONEMORESTEP:
    reset;
    LOOP:
    *(result+i)=*(result+i+1);
    if (i++>=len(result)-1) goto WEDIDIT;
    goto LOOP;
WEDIDIT:
    *(result+i)='\0';
    return result;
}
int main(int argc, char*argv[])
{
    const char*str="Foo!";
    char*reverse=reversed(str);
    printf("%s\n",reverse);
    free(reverse);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

WEDIDIT 🎉