about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/3/Ex1.c
blob: 90b3ca53b39234cbe3774f91cfc05138bbd3bde9 (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
/*
 * Reverse name from stdin.
 * This is free and unencumbered software released into the public domain.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "stack.h"

int main()
{
	stack *s = mkstack();
	char c, *p;

	while ((c = getchar()) != EOF) {
		p = malloc(sizeof(char *));
		*p = isspace(c) ? 32 : c;
		stack_push(s, p);
	}

	while (!stack_empty(s) && *(char *) stack_top(s) == 32) {
		p = stack_pop(s);
		free(p);
	}

	c = 32;
	while (!stack_empty(s)) {
		p = stack_pop(s);
		putchar(c == 32 ? toupper(c = *p) : tolower(c = *p));
		free(p);
	}

	putchar(10);

	return 0;
}