93. Reverse a String In-Place

Back To All Submissions
Previous Submission
Next Submission

Code

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

void reverse_string(char *str) {
    // Your logic here
    int head = 0;
    int tail = strlen(str) - 1;
    while (head < tail) {
        char temp = str[head];
        str[head] = str[tail];
        str[tail] = temp;
        head++; tail--;
    }
}

int main() {
    char str[101];
    fgets(str, sizeof(str), stdin);

    // Remove newline
    int i = 0;
    while (str[i] != '\0') {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
        i++;
    }

    reverse_string(str);
    printf("%s", str);
    return 0;
}

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote