2
2
* Exercise 1-19. Write a function reverse(s) that reverses the character
3
3
* string s. Use it to write a program that reverses its input a line at a
4
4
* time.
5
+ *
5
6
* By Faisal Saadatmand
6
7
*/
7
8
8
9
#include <stdio.h>
9
10
10
11
#define MAXLINE 1000 /* maximum input line length */
11
- #define YES 1
12
- #define NO 0
13
12
14
13
/* functions */
15
14
int getLine (char [], int );
16
- int delTrailingWS (char [], int );
17
15
void reverse (char [], char [], int );
16
+ void emplace_reverse (char [], int );
18
17
19
18
/* getLine function: read a line into s, return length */
20
19
int getLine (char s [], int lim )
@@ -34,64 +33,39 @@ int getLine(char s[], int lim)
34
33
return i ;
35
34
}
36
35
37
- /* delTrailingWS function: deletes trailing whitespaces and blank lines */
38
- int delTrailingWS (char s [], int len )
36
+ /* reverse function: copy the charaters of s1 into s2 in reverse order */
37
+ void reverse (char s1 [], char s2 [], int len )
39
38
{
40
- int i , blankLine ;
41
- int newLength = len ; /* new length of line after whitespaces removal */
42
-
43
- i = 0 ;
44
- blankLine = NO ;
45
-
46
- /* test for blanklines with witespaces */
47
- while (s [i ] == ' ' || s [i ] == '\t' || s [i ] == '\n' )
48
- ++ i ;
49
- if (i == len ) { /* remove whitespaces spaces form blank lines */
50
- s [0 ] = '\n' ;
51
- for (i = 1 ; s [i ] != '\0' ; ++ i )
52
- s [i ] = '\0' ;
53
- newLength -= i - 1 ;
54
- }
39
+ int i ;
55
40
56
- if ( s [ 0 ] == '\n' ) {
57
- blankLine = YES ;
58
- }
41
+ -- len ; /* skip the null character '\0' */
42
+ for ( i = 0 ; s1 [ i ] != '\0' ; ++ i , -- len )
43
+ s2 [ len ] = s1 [ i ];
59
44
60
- if (!blankLine ) { /* skip entirely blank lines */
61
- while (s [len ] == '\0' || s [len ] == '\n' ||
62
- s [len ] == ' ' || s [len ] == '\t' ) {
63
- s [len ] = '\0' ;
64
- -- len ;
65
- }
66
- s [len + 1 ] = '\n' ; /* insert newline char before null char */
67
- newLength = len + 2 ; /* plus 2 for the newline and null characters */
68
- }
69
- return newLength ;
45
+ s2 [i ] = '\0' ;
70
46
}
71
47
72
- /* reverse function : reverses char string s1[] and store values into s[2] */
73
- void reverse (char s1 [], char s2 [], int len )
48
+ /* emplace_reverse : reverses s's charaters in-place. */
49
+ void emplace_reverse (char s [], int len )
74
50
{
75
- int i = 0 ;
51
+ int i , j , temp ;
76
52
77
- for (-- len ; s1 [i ] != '\0' ; ++ i ) {
78
- s2 [len ] = s1 [i ];
79
- -- len ;
53
+ for (i = 0 , j = len - 1 ; i < len / 2 ; ++ i , -- j ) {
54
+ temp = s [i ];
55
+ s [i ] = s [j ];
56
+ s [j ] = temp ;
80
57
}
81
- s2 [i ] = '\0' ;
82
58
}
83
59
84
60
int main (void )
85
61
{
86
62
int len ; /* current line length */
87
63
char line [MAXLINE ]; /* current input line */
88
- char reversedLine [MAXLINE ];
89
64
90
65
while ((len = getLine (line , MAXLINE )) > 0 ) {
91
- len = delTrailingWS ( line , len ) ; /* new length after removing ws */
92
- reverse (line , reversedLine , len );
93
- printf ("%s" , reversedLine );
66
+ line [ -- len ] = '\0' ; /* remove newline character at the end */
67
+ emplace_reverse (line , len );
68
+ printf ("%s\n " , line );
94
69
}
95
- printf ("\n" );
96
70
return 0 ;
97
71
}
0 commit comments