Added example 5-14. Sorts data from stdin. Has some issues.
[C-Programming-Examples.git] / ex_4-13.c
blob1eaf851a7ad51a14ff00729fa14f2ca2870df8bd
1 #include <string.h>
2 #include <stdio.h>
4 /* print contents of array */
5 void print_array(char s[])
7 int i;
8 for(i = 0; i < strlen(s); i++)
9 printf("%c", s[i]);
10 printf("\n");
13 /* reverse contents of array in place */
14 void rev_array(char s[])
16 int c, i, j;
17 for(i = 0, j = strlen(s)-1; i < j; i++, j--)
19 c = s[i];
20 s[i] = s[j];
21 s[j] = c;
25 /* helper function - performs the recursion */
26 int rec_rev(char s[], int i, int j)
28 char c;
30 c = s[i];
31 s[i] = s[j];
32 s[j] = c;
34 if(i < j)
36 i++;
37 j--;
38 rec_rev(s, i, j);
41 return 1;
44 /* reverse contents of array recursively in place */
45 void rev_array_rec(char s[])
47 int i = 0;
48 int j = strlen(s)-1;
49 rec_rev(s, i, j);
53 int main()
55 char s[10] = { "Reverse Me" };
57 print_array(s);
58 rev_array_rec(s);
59 print_array(s);
60 return 1;