String Reverse In-place

String Reverse In-place

Easy C Strings 40 views
Explanation Complexity

Problem Statement

Reverse a string without extra arrays. Two pointer technique: swap characters from the start and end until they meet in the middle.

Input Format

A string entered by the user.

Output Format

The same string reversed in-place.

Example

"computer"
"retupmoc"

Constraints

• No extra array allowed

• In-place modification only

Concept Explanation

The string is reversed by swapping characters from the start and end using two pointers.

Step-by-Step Explanation

1.Take the input string.

2.Set pointer start at the first character.

3.Set pointer end at the last character.

4.While start < end:

5.Swap characters at start and end.

6.Move start one step forward.

7.Move end one step backward.

8.Stop when both pointers meet.

9.The string is reversed in the same memory.

Concept Explanation

The string is reversed by swapping characters from the start and end using two pointers.

Step-by-Step Explanation

1.Take the input string.

2.Set pointer start at the first character.

3.Set pointer end at the last character.

4.While start < end:

5.Swap characters at start and end.

6.Move start one step forward.

7.Move end one step backward.

8.Stop when both pointers meet.

9.The string is reversed in the same memory.

Input / Output Format

Input Format
A string entered by the user.
Output Format
The same string reversed in-place.
Constraints
• No extra array allowed

• In-place modification only

Examples

Input:
"computer"
Output:
"retupmoc"

Example Solution (Public)

C
#include <stdio.h>

int main() {
    char str[100];
    int start = 0, end, i;
    char temp;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Find length manually
    end = 0;
    while (str[end] != '�') {
        end++;
    }

    // Remove newline if present
    if (str[end - 1] == 'n') {
        end--;
    }

    end = end - 1;   // index of last character

    // Two-pointer swap
    while (start < end) {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;

        start++;
        end--;
    }

    printf("Reversed string: %sn", str);

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    char str[100];
    int start = 0, end, i;
    char temp;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Find length manually
    end = 0;
    while (str[end] != '�') {
        end++;
    }

    // Remove newline if present
    if (str[end - 1] == 'n') {
        end--;
    }

    end = end - 1;   // index of last character

    // Two-pointer swap
    while (start < end) {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;

        start++;
        end--;
    }

    printf("Reversed string: %sn", str);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.