-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise09.c
78 lines (58 loc) · 1.27 KB
/
exercise09.c
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
71
72
73
74
75
76
77
78
// C Primer Plus
// Chapter 11 Exercise 9
// Write a function that replaces the contents of a string with the string
// reversed. Test the function in a complete program that uses a loop to
// provide input values for feeding to the function.
#include <stdio.h>
#define LIMIT 50
void reverse_string(char *string);
char * get(char *string, int n);
int main(void)
{
// test reverse_string()
char string[LIMIT];
printf("Enter a string to reverse: ");
get(string, LIMIT);
while (string[0] != '\0')
{
reverse_string(string);
printf("Your string reversed: %s\n", string);
printf("Enter a string to reverse (empty line to quit): ");
get(string, LIMIT);
}
puts("Bye");
return 0;
}
void reverse_string(char *start)
{
// replace contents of a string with the string reversed
char *end = start;
char tmp;
// find location of the end of the string
while (*(end + 1) != '\0')
end++;
// switch characters
while (start < end)
{
tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}
char * get(char *string, int n)
{
// wrapper for fgets that replaces first newline with null
char *return_value = fgets(string, n, stdin);
while (*string != '\0')
{
if (*string == '\n')
{
*string = '\0';
break;
}
string++;
}
return return_value;
}