Start development on BSD 4
[unix-history] / .ref-5cb41021d721f4e0ac572d592613f963e495d1ff / .ref-BSD-3 / usr / lib / learn / C / L31.1a
CommitLineData
fd15fd37
BJ
1#print
2Write a function named "rev(s)" which reverses
3the string "s" in place. Name the file that contains
4the function "rev.c".
5When you're satisfied, type "ready".
6#once #create Ref
7cbax0987654321
8#once #create tzaqc.c
9main(){
10 char *s1, *s2, *s3, *s4;
11 s1 = "abc";
12 s2 = "x";
13 s3 = "";
14 s4 = "1234567890";
15 rev(s1);
16 rev(s2);
17 rev(s3);
18 rev(s4);
19 printf(s1);
20 printf(s2);
21 printf(s3);
22 printf(s4);
23 printf("\n");
24}
25#user
26cc tzaqc.c rev.o
27a.out >value
28#cmp value Ref
29#succeed
30One way:
31
32rev (s)
33char *s;
34{
35 char *p;
36 int t;
37
38 for (p=s; *p; p++)
39 ;
40 for (p--; p > s; p--, s++) {
41 t = *p;
42 *p = *s;
43 *s = t;
44 }
45}
46#log
47#next
4832.1a 10