Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_1.18_remtrailbt.c
1240 views
1
/**
2
*
3
* Exercise 1.18 - Write a Program to remove the trailing blanks and tabs
4
* from each input line and to delete entirely blank lines
5
*
6
**/
7
8
#include<stdio.h>
9
10
#define MAXLINE 1000
11
12
int mgetline(char line[],int lim);
13
int removetrail(char rline[]);
14
15
int main(void)
16
{
17
int len;
18
char line[MAXLINE];
19
20
while((len=mgetline(line,MAXLINE))>0)
21
if(removetrail(line) > 0)
22
printf("%s",line);
23
24
return 0;
25
}
26
27
28
int mgetline(char s[],int lim)
29
{
30
int i,c;
31
32
for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
33
s[i] = c;
34
if( c == '\n')
35
{
36
s[i]=c;
37
++i;
38
}
39
s[i]='\0';
40
41
return i;
42
}
43
44
/* To remove Trailing Blanks,tabs. Go to End and proceed backwards removing */
45
46
int removetrail(char s[])
47
{
48
int i;
49
50
for(i=0; s[i]!='\n'; ++i)
51
;
52
--i; /* To consider raw line without \n */
53
54
for(i >0; ((s[i] == ' ') || (s[i] =='\t'));--i)
55
; /* Removing the Trailing Blanks and Tab Spaces */
56
57
if( i >= 0) /* Non Empty Line */
58
{
59
++i;
60
s[i] = '\n';
61
++i;
62
s[i] = '\0';
63
}
64
return i;
65
}
66
67