You are on page 1of 1

#include <stdio.

h>
#include <string.h>

// Function to reverse a C-string without using pointers arithmetic


void reverse(char* str)
{
// get the length of the string
int n = strlen(str);

// start swapping characters from both ends of the string


for (int i = 0, j = n - 1; i < j; i++, j--)
{
char ch = str[i];
str[i] = str[j];
str[j] = ch;
}
}

int main(void)
{
char str[] = "Reverse me";

reverse(str);
printf("%s", str);

return 0;
}

You might also like