aboutsummaryrefslogtreecommitdiff
path: root/1.19.c
blob: c156afedd5e25fd30989711a20c4bf32f6746fa7 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>

#define MAXLINE 1000

void reverse(char str[]);
void str_cp(char to[], char from[]);

int
main(void)
{
	int input, i;
	char str[MAXLINE];

	i = 0;

	while ((input = getchar()) != EOF) {
		str[i] = input;
		i++;

		if (input == '\n') {
                        str[i] = '\0';
			i = 0;
			reverse(str);
                        printf("%s\n", str);
		}
	}

	return 0;
}

void 
str_cp(char to[], char from[])
{
	int i, last_non_blank;

        last_non_blank = -1;

	for (i = 0; from[i] != '\0'; ++i) {
          if (from[i] != ' '&& from[i] != '\n' && from[i] != '\t')
            last_non_blank = i;

          to[i] = from[i];
        }
		
        if (last_non_blank >= 0)
          to[last_non_blank+1] = '\0';
        else
          to[i] = '\0';
}

void
reverse(char str[])
{
	int i, len;
	char storage[MAXLINE];

        i = len = 0;

	str_cp (storage, str);
	
	for ( len = 0; storage[len] != '\0'; len++)
		;

	for ( i = 0; i < len; ++i)
		str[i] = storage[len-1-i];

	str[i] = '\0';
}