how to reverse string in c language

how to reverse string in c language

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);
}
Sourc‮w:e‬ww.lautturi.com

output:

lautturi
moc.iruttual
Created Time:2017-08-29 08:52:10  Author:lautturi