C - Reverse character string

Share a big cow's artificial intelligence tutorial. Zero-based! Easy to understand! Funny and humorous! Hope you join the artificial intelligence team too! Please click http://www.captainbed.net

/*
 * Write a function Reverse(s) that reverses the character string s.
 *
 * Reverse.c - by FreeMan
 */

#include <stdio.h>

#define MAXLEN 128 /* Maximum length of the string */

void Reverse(char s[]);

int main()
{
	char s[] = "Write a function reverse(s) that reverses the character string s.";
	Reverse(s);
	return 0;
}

void Reverse(char s[])
{
	int i, j;
	char line[MAXLEN];

	for (i = 0; s[i] != '\0' && i < MAXLEN; ++i)
	{
	}

	j = 0;
	if (i == 0 || s[i] == '\0')
	{
		line[i] = '\0';
	}

	if (i > 0)
	{
		for (--i; i >= 0; --i)
		{
			line[i] = s[j++];
		}
	}
	printf("%s\n", line);
}

// Output:
/*
.s gnirts retcarahc eht sesrever taht )s(esrever noitcnuf a etirW

*/

 

Guess you like

Origin blog.csdn.net/chimomo/article/details/112882590