I`m learning with the Kernighan and Ritchie the C programming language and I need to write a function that reverses the character string s. Then I need to test it.
Here is what I came up with so far but didn`t work right.
always use simplest approach which 1st come to mind, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void reverse(char s[])
{
int len = strlen(s); // get string length
char * end = s + len - 1; // find pointer to last byte; -1 for '\0'
int i;
char temp;
for (i = 0; i < len / 2; i++)
{
// swap bytes
temp = *(end - i);
*(end - i) = s[i];
s[i] = temp;
}
s[len] = '\0';
}
Thank you , but we have not covered pointers or the string.h library yet. With the basic toolkit of the book provided so far, I would expect something like this: