Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable-Less String reverse [duplicate]

In a recent interview, I was asked a very simple question to reverse a string (not just print) without any extra variable and any in-built function. The closest I could think of is:

#include<stdio.h>
#include<string.h>
int main()
{
    char ch[100];
    scanf("%s",&ch);
    int i=0;
    while(i<strlen(ch)/2)
    {
       ch[i]=ch[strlen(ch)-1-i]+ch[i];
       ch[strlen(ch)-1-i]=ch[i]-ch[strlen(ch)-1-i];
       ch[i]=ch[i]-ch[strlen(ch)-1-i];
       i++;
    }
    printf("%s",ch);
    return 0;
}

My solution was rejected as I used variable i. How this is even possible without using a counter variable? Is there any other method to solve this?

EDIT

These were the exact words of question(nothing more or less):

Reverse a string without using any variable or inbuilt functions in C.

like image 841
wrangler Avatar asked Nov 02 '25 15:11

wrangler


1 Answers

Two Three possible implementations: one only prints the string in reverse. Another reverses the string in-memory and in-place. Both assume that defining your own recursive functions is allowed and that parameters don't count as variables. In any case, the parameters themselves are constant, so arguably not variables.

void printRev(const char * const s){
    if(*s != '\0'){ // or just: if(*s){
        printRev(s + 1);
        putchar(*s);
    }
}

Does 'prefix' recursion through the string: first recurse until the end is reached, then print each character after the recursive call returns.

void revStr(char * const s, const int len){
    if(len > 0){
        if(s[0] != s[len]){
            s[0] ^= s[len];
            s[len] ^= s[0];
            s[0] ^= s[len];
        }

        revStr(s + 1, len - 2);
    }
}

Is slightly more involved: it XOR-swaps the 'first' character of the string with the 'last'. Then recurses with the next character as the start of the string, and the length decreased by two. So in the next iteration the second character becomes the first, and the second to last becomes the last character. For this the s pointer itself is still const, but obviously the characters pointed to are modified.

The second function requires the string length as an input parameter, this can also be done (recursively) without needing the built-in strlen function:

int myStrlen(const char * const s){
    if(*s != '\0'){
        return 1 + myStrlen(s + 1);
    }

    return 0;
}

Addit

Here is a version that does not use a length parameter, but requires a disjoint output string, and the input string to be modifiable. It simulates the len - 2 expression from revStr by replacing the last character in src with a NUL-character.

void copyRev(char * const restrict dst, char * const restrict src){
    if(src[0] != '\0'){
        dst[0] = src[myStrlen(src) - 1];
        dst[myStrlen(src) - 1] = src[0];
        src[myStrlen(src) - 1] = '\0';

        copyRev(dst + 1, src + 1);
    }
}
like image 65
Kninnug Avatar answered Nov 04 '25 10:11

Kninnug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!