blob: ef586fa699c3294ee36881560d8fd5f396932bad (
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
43
44
45
46
47
48
49
50
51
52
53
|
#include <stdio.h>
#define MAXLEN 1000
int mygetline(char s[], int max);
void reverse(char s[], int size);
/* reverses input string */
int main(int argc, char *argv[]) {
int size;
char s[MAXLEN];
while ((size = mygetline(s, MAXLEN)) > 0) {
reverse(s, size);
printf("%s\n", s);
}
if (size == -1) {
printf("exceeded max input size: %d\n", MAXLEN - 1);
return 1;
}
return 0;
}
int mygetline(char s[], int max) {
int c, i;
for (i = 0; i < max - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
s[i] = '\0';
if (i == max - 1 && c != '\n' && c != EOF) {
c = getchar();
if (c != '\n' && c != EOF)
return -1;
}
return i;
}
void reverse(char s[], int size) {
int i, j, upper;
upper = size / 2;
for (i = 0; i < upper; i++) {
j = size - i - 1;
s[i] = s[i] ^ s[j];
s[j] = s[i] ^ s[j];
s[i] = s[i] ^ s[j];
}
}
|