0

I have a reversing-a-string program below which didn't work. A line "str[start] = str[end];" in the while loop caused it to end prematurely (it's marked down below).

Any pointers appreciated.

#include <iostream>
using namespace std;

int main() {
    int end, start;
    char temp;
    char* str = "hello";
    start = 0;
    end = strlen(str) - 1;

    while(end > start)
    {
        temp = str[start];
        str[start] = str[end];   // <------------- This line caused issue
        str[end] = temp;
        start++; end--;
    }

    // print out string
    int i = 0;
    while (str[i])
        printf("%c\n", str[i++]);
}
user1972031
  • 547
  • 1
  • 5
  • 19

0 Answers0