-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise10.c
78 lines (58 loc) · 1.32 KB
/
exercise10.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 10:
// Write a function that takes a string as an argument and removes the spaces
// from the string. Test it in a program that uses a loop to read lines until
// you enter an empty line. The program should apply the function to each input
// string and display the result.
#include <stdio.h>
#define LIMIT 50
void remove_spaces(char *string);
char * get(char *string, int n);
int main(void)
{
// test remove_spaces()
char string[LIMIT];
printf("Test remove_spaces()\n");
printf("Enter a string: ");
get(string, LIMIT);
while (string[0] != '\0')
{
remove_spaces(string);
printf("Your string, without spaces: %s\n", string);
printf("Enter a string (empty line to quit): ");
get(string, LIMIT);
}
puts("Bye");
return 0;
}
void remove_spaces(char *string)
{
// remove all spaces from a string
unsigned long spaces_found = 0;
while (1)
{
if (*string == ' ')
spaces_found++;
else
*(string - spaces_found) = *string;
// if end of string, break
if (*string == '\0')
break;
string++;
}
}
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;
}