summaryrefslogtreecommitdiff
path: root/reverse.c
blob: a038df258c2c7a8671fdb737bff0a427683f8090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <u.h>
#include <libc.h>

char *
reverse(char *s, int n)
{
	int i;
	char tmp;

	for(i = 0; i < n/2; i++){
		tmp = s[i];
		s[i] = s[n-i-1];
		s[n-i-1] = tmp;
	}
	return s;
}

char *
reversep(char *s, int n)
{
	char *bp, *ep;
	char tmp;

	for(bp = s, ep = s+n-1; bp != ep; bp++, ep--){
		tmp = *bp;
		*bp = *ep;
		*ep = tmp;
	}
	return s;
}

void
main()
{
	char s[] = "hello";

	print("%s → ", s);
	print("%s\n", reverse(s, strlen(s)));
	print("%s → ", s);
	print("%s\n", reversep(s, strlen(s)));
	exits(0);
}