-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_input.c
More file actions
52 lines (41 loc) · 857 Bytes
/
read_input.c
File metadata and controls
52 lines (41 loc) · 857 Bytes
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
/* read_input.c
* ENSF 337 Lab 4 Exercise E
*
*/
#include "read_input.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
int get_string(char* digits, int n)
{
int i = 0, j;
int length;
if(fgets(digits, n, stdin) == NULL)
return EOF;
/* If there is a newline character in the string replace it with '\0'. */
length = strlen(digits);
if (digits[length - 1] == '\n') {
digits[length - 1] = '\0';
length--;
}
/* remove leading spaces */
i = 0;
while(digits[i] == ' ')
i++;
length -= i; /* number of leading spaces == i. */
j = 0;
while (j <= length) {
digits[j] = digits[i];
i++;
j++;
}
/* remove trailing spaces */
if (length > 0) {
j = length - 1;
while (digits[j] == ' ')
j--;
length = j + 1;
digits[length] = '\0';
}
return 1;
}