-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise1-18.c
64 lines (52 loc) · 1.28 KB
/
Exercise1-18.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
/*Exercise 1-18. Write a program to remove trailing blanks and tabs
from each line of input, and to delete entirely blank lines.*/
#include <stdio.h>
#define MAXLINE 5000 /* maximum input line length */
int get_line(char line[], int maxline);
void elimate_blanks_ends(const char s[],char new_line[]);
/* print the longest input line */
main()
{
int len; /* current line length */
char line[MAXLINE]; /* current input line */
char new_line[MAXLINE];
while ((len = get_line(line, MAXLINE)) > 0)
if (len > 0)
{
elimate_blanks_ends(line,new_line);
printf("%s",new_line);
}
return 0;
}
/* getline: read a line into s, return length */
int get_line(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void elimate_blanks_ends(const char s[],char new_line[])
{
int i,j=0;
while(s[j]!='\0')
++j;
int flag=0;
for(i=j; i>=0; i--)
{
if( (s[i]==' '|| s[i]=='\t' || s[i]=='\n' || s[i]=='\0') && (!flag) )
{
//noting
}
else
{
flag=1;
new_line[i]=s[i];
}
}
}