Agregar carácter al final de la línea en C

I'm sure there is a very simple answer to this question. However, I am trying to add a character to the beginning and the end of a line in C. I have the add at the beginning part down but am having issues with adding the add to the end part. Is there a simple solution just using the code I have? Thanks in advance

#include <stdio.h>

int main(void) {
   char c; // store a character

   printf("0");
   while (scanf("%c", &c) != EOF) {
      printf("%c", c);
      if (c == '\n') {
         printf("0");
      } // if
   } // while

   return 0;

} // main

preguntado el 01 de febrero de 12 a las 03:02

What do you expect to happen, and what actually happens? If the problem is that you get one long line of output, add printf("\n"); just before the closing brace of the if. -

1 Respuestas

Necesitas comprobar newline antes de printing the character, something like this:

while (scanf("%c", &c) != EOF) {
    if (c == '\n') {
        printf("0");
    }
    printf("%c",c);
}

This code will print your new character before the newline.

Respondido 01 Feb 12, 07:02

Thanks, Dawood. I knew there was an easy solution to this! - James

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.