0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * inplace_rev(char * s){
    int n=strlen(s);
    int i=0;
    char aux,aux2;
    while(i<=n/2){
        aux=s[n-i-1];
        aux2=s[i];
        s[i]=aux;
        s[n-i-1]=aux2;
        i++;
    }
    return s;
}
int main(int argc, char *argv[]) {
    printf("%d\n",sizeof(int));
    char *s=(char *)malloc(100);
    s="abcdef";
    printf("%s",s);
    printf("%s\n%s",s,inplace_rev(s));
    return 0;
}

The segmentation fault happens at the first iteration of the while structure in method "inplace rev". More specifically, at

s[i]=aux;

I used two aux variables to emphasize the segmentation fault. Basically, I'm trying to reverse a string in place. Thanks in advance ! It

0 Answers0