K
kyle.tk
I am trying to make a function to reverse a string. What I have right
now ends in a segfault.
#include <string.h>
#include <stdio.h>
/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}
int main(void){
char *str = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}
now ends in a segfault.
#include <string.h>
#include <stdio.h>
/* Reverse a string */
void strev(char *s){
char tmp;
int f = 0;
int l = strlen(s) - 1;
while (f < l){
tmp = s[f];
s[f] = s[l];
s[l] = tmp;
f++;l--;
};
return;
}
int main(void){
char *str = "kyle";
strev(str);
printf("s = %s\n",str); /* I want the to output elyk */
return 0;
}