Sunday, 13 October 2013

program to print a string backwards

  /* program to print lines backwards */

    #include <stdio.h>

    # define BUFFERSIZE 80
    main()
    {
       char buffer[BUFFERSIZE];
       char *bp;   /* a pointer to a character */
       int k, j;      /* loop induction variables */

       bp = buffer;
       while ( fgets(buffer, BUFFERSIZE, stdin) != NULL ) {
           /* buffer has one line of input */
           printf("the line backwards:\n");

           /*  find the end of the line */
           k = 0;
           while ( *(buffer+k) != '\0' ) k++;

           k--;
           if ( (k >= 0) && (*(buffer+k) == '\n') ) k--;

           /*  print relevant characters in reverse order */
           for ( j = k; j >= 0; j-- ) {
               printf("%c", *(buffer + j));
           }
           printf("\n");
       }
       return(0);
    }

No comments:

Post a Comment