-2

I'm beggining to learn C but i'm stuck in this exercise, i have to implement a function that reverses a string (in place) and i really don't know what i'm doing wrong. Any help will be welcome, thanks!

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

void invertir(char* cadena){
    char aux;
    int i = 0;
    int j = strlen(cadena) / 2;
    while(i < j){
        aux = cadena[i];
        cadena[i] = cadena[j];
        cadena[j] = aux;
        i++;
        j--;
    }
    printf("La palabra invertida es: %s\n",cadena );
}

void main(){
    return invertir("parlante");
}

it gives me 'segmentation fault', i'm sure it's a rookie mistake or something i forget to do. so thanks for the patience!

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 7
    You can't modify the string `"parlante"` directly. You need to copy to a writable array of `char`. Also, I don't think you want to divide by 2 in `int j = strlen(cadena) / 2;`. – Thomas Jager Jul 19 '19 at 17:24
  • 1
    And how I'm supposed to do that?. Sorry again about the ignorance. – Fran Bricchi Jul 19 '19 at 17:33
  • Look up the function `strcpy`. You'll want to create an array that's big enough for you string in main, and use `strcpy`. Alternatively, just do `char my_string[] = "parlante"; return invertir(my_string);` – Thomas Jager Jul 19 '19 at 17:34
  • Be careful about the function types. `void invertir` does not return a value, but in `main` you are using a supposed return value. Function `main()` too should be `int main(void)`. – Weather Vane Jul 19 '19 at 17:48
  • Regardless of the algorithm correctness, read this: [Why does writing to a string literal in this C program segfault?](https://stackoverflow.com/questions/3638851/why-does-writing-to-a-string-literal-in-this-c-program-segfault) – WhozCraig Jul 19 '19 at 17:53
  • Thanks for all the advices to everyone! – Fran Bricchi Jul 19 '19 at 17:55
  • You also do not want to start reversing the string with `j = strlen(cadena) / 2;`. Think about it... You are trying to reverse a string by starting with `i = 0;` and `j` at the middle? – David C. Rankin Jul 20 '19 at 03:03

1 Answers1

1

Don't write to string constants. Write to string arrays. Yes I know they're of type char* for legacy reasons so the compiler won't catch you if you do this.

Do not write:

void main(){
    return invertir("parlante");
}

write:

void main(){
    char string[] = "parlante"
    invertir(string);
    puts(string);
}

Also, your program clearly doesn't compile because return void;

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 1
    You may also like to mention the use of the *non-conforming* `void main()`. See: [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Jul 20 '19 at 03:05
  • @DavidC.Rankin: He's probably using `gcc` which still accepts `void main`. – Joshua Jul 20 '19 at 04:03
  • That is the difference between a *"conforming"* implementation and an *"implementation defined"* system. Many embedded systems have no OS and will be non-conforming. See [§5.1.2.1](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.1) and [§5.1.2.2](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2) – David C. Rankin Jul 20 '19 at 04:52