How to reverse a string
/*
Example: reverse a string in C language
*/
#include <stdio.h>
#include <stdlib.h>
void reverse(char *str)
{
char *r_ptr = str;
while (*(r_ptr + 1) != '\0')
r_ptr++;
while (r_ptr > str)
{
char tmp = *r_ptr;
*r_ptr-- = *str;
*str++ = tmp;
}
}
int main(int argc, char const *argv[]) {
char str[20] = {'l','a','u','t','t','u','r','i','.','c','o','m'};
puts(str);
reverse(str);
puts(str);
}Sourcw:eww.lautturi.comoutput:
lautturi moc.iruttual