Reverse a String In-Place

Code

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

void reverse_string(char *str){
    if(str == NULL){
        return; 
    }
    int count = 0;
    while(str[count] != '\0'){
        count++; 
    }
    int l = 0, r = count - 1;
    while(l <= r){
        int tmp = str[l];
            str[l] = str[r];
            str[r] = tmp; 
            l++;
            r--;
    }
}

int main(){
    char str[101];
    fgets(str, sizeof(str), stdin);
    str[strcspn(str,"\n")] = '\0';
    reverse_string(str);
    printf("%s",str);
    return 0;
} 

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

firmware

Expected Output

erawmrif