#include <stdio.h>
void reverse(char *st, int length);
int strl(char *str);
int main (void)
{
// here if str is an array the code will work
char *str = "future video";
printf("%s\n", str);
reverse(str, strl(str));
printf("%s\n", str);
}
This is a function for counting the length of the string
int strl(char *str)
{
int offset = 0;
while (str[offset] != 0)
{
offset++;
}
return offset;
}
This is a function to reverse the string
void reverse(char *st, int length)
{
char tmp;
for (int i = 0; i < length / 2; i++)
{
tmp = st[i];
//I used gdb and the seg fault happens in this line
st[i] = st[length-1-i];
st[length-1-i] = tmp;
}
}