[Solved] Code to reverse a string: “Undefined symbols for architecture x86_64” [closed]


The compiler complains about

char* reverse(string input);

is not defined but

char* reverse(char* input);

is.

Unrelated fault.

char* reverse(char* input) {
  char* reversed;
  reversed = new char(sizeof(input) - 1); // <----------- 2 FAULT here
  for(int i = 0; i != '\0'; i++) { // <--- and here
    char* j = reversed + sizeof(input - 1); // <-- and here
    input[i] = *j; // <--- and here
    j++; // <--- and here
  }
  return reversed;
}

Should have been

char* reverse(char* input) {
  char* reversed;
  reversed = new char[strlen(input) + 1];  // new array with room for nul
  char* j = input + strlen(input) - 1; // last char in input
  for(int i = 0; input[i] != '\0'; i++) {
    reversed[i] = *j;
    j--;
  }
  reversed[strlen(input)] = '\0';
  return reversed;
}

8

solved Code to reverse a string: “Undefined symbols for architecture x86_64” [closed]