K&R exercise 1-19: reversing each line of input This is my solution for exercise 1-19 of K&amp;R:


  Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.1


It works, but I'm trying to write good programs. I think the fact that I can't use the length of the vector when already got the line in function getlinea makes me think I could do better. The function reverse(s) uses 3 for sentences, which might be a little ugly. I know it doesn't mean it's not good but having to "read" the line 4 times is kind of a little stupid to me. I still can't think of a different solution.

/*
 * main.c
 *
 *  Created on: 19/3/2015
 *      Author: utnso
 */

#include &lt;stdio.h&gt;

#define MAXLINE 1000

void reverse (char s [] );
int getlinea (char s [] , int lim);

int main ()
{
    int len;
    char palabra [MAXLINE];
    while ( ( len = getlinea ( palabra, MAXLINE ) )  &gt;= 0 )
        if (len &gt; 0)
            printf ("%s\n", palabra);
    return 0;

}

int getlinea ( char s [], int lim )
{
    int i, c;
    i = 0;
    while ( ( c = getchar() ) != EOF &amp;&amp; c != '\n' &amp;&amp; i &lt; lim-1 )
    {
        s [i] = c ;
        ++ i;
    }
    s [i] = '\0';
    if ( i &gt; 0 )
        reverse ( s ) ;
    else
        if ( c == EOF )
            return -1;
    return i ;
}

void reverse ( char turnaround [] )
{
    int i, j;
    int h = 0;
    char aux [MAXLINE];
    for ( i = 0; turnaround [i] != '\0' ; ++i )
        ;
    for ( j = i-1 ; j &gt;= 0 ; --j)
    {
        aux [h] = turnaround [j];
        ++h;
    }
    for ( i = 0 ; turnaround [i] != '\0' ; ++i )
        turnaround [i] = aux [i];
}


This is the output for


$ ./1-19 &lt; main.c



(it also deletes white lines)


*/
c.niam * 
* 
5102/3/91 :no detaerC  * 
osntu :rohtuA      * 
/* 
&gt;h.oidts&lt; edulcni#
0001 ENILXAM enifed#
;) ][ s rahc( esrever diov
;)mil tni , ][ s rahc( aenilteg tni
)( niam tni
{
;nel tni  
;]ENILXAM[ arbalap rahc   
) 0 =&gt;  ) ) ENILXAM ,arbalap ( aenilteg = nel ( ( elihw   
)0 &gt; nel( fi      
;)arbalap ,"n\s%"( ftnirp         
;0 nruter 
}
) mil tni ,][ s rahc ( aenilteg tni
{
;c ,i tni 
;0 = i    
) 1-mil &lt; i &amp;&amp; 'n\' =! c &amp;&amp; FOE =! ) )(rahcteg = c ( ( elihw  
{ 
; c = ]i[ s       
;i ++     
} 
) 0 &gt; i ( fi  
{ 
;'0\' = ]i[ s     
; ) s ( esrever       
} 
esle  
) FOE == c ( fi       
;1- nruter            
; i nruter    
}
) ][ dnuoranrut rahc ( esrever diov
{
;j ,i tni 
;0 = h tni    
;]ENILXAM[ xua rahc   
) i++ ; '0\' =! ]i[ dnuoranrut ;0 = i ( rof   
;     
)j-- ; 0 =&gt; j ; 1-i = j ( rof 
{ 
;]j[ dnuoranrut = ]h[ xua     
;h++      
} 
) i++ ; '0\' =! ]i[ dnuoranrut ; 0 = i ( rof  
;]i[ xua = ]i[ dnuoranrut     
}





1http://cs.indstate.edu/~cbasavaraj/cs559/the_c_programming_language_2.pdf -
 Page 31
getlinea does too much: it reads the line and reverses it. A Single Responsibility Principle mandates restructuring the code:

while((len = getlinea(...)) &gt; 0) {
    reverse();
    print();
}

Avoid naked loops. Each loop represent an important action, and deserves a name. The code:

reverse(char turnaround[])
{
    char aux[...];
    int len = length_of_string(turnaround);
    copy_backwards(aux, turnaround, len);
    copy(turnaround, aux, len);
}


is much easier to reason about.
The length of the input is known to the caller. Consider passing it as parameter:

reverse(char turnaround[], int len)
{
    char aux[...];
    copy_backwards(aux, turnaround, len);
    copy(turnaround, aux, len);
}

Finally, a string can be reversed in place. There is no need for aux. I hope you can figure a solution yourself.