All submissions

Reverse a String In-Place

 

#include <stdio.h>

void reverse_string(char *str) {
    // Your logic here
    // Find the length of the string
    int length = 0;
    while (str[length] != '\0') 
    {
        length++;
    }
    
    // Swap characters from both ends
    int start = 0;
    int end = length - 1;
    
    while (start < end) 
    {
        // Swap characters at start and end positions
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        
        // Move pointers towards center
        start++;
        end--;
    }
}

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;
}

 

 

 

Loading...

Input

firmware

Expected Output

erawmrif