Truncar una cadena de texto dada por el usuario en C

For my assignment I was asked to receive data from standard input and print it back out through standard output. So far I have that done correctly. (Code below)

int main (void)
int a;
while ( ( a = getchar () ) != EOF)
return 0;

Now step two asks me to truncate the line, that is, once it reaches 72 characters in a line, the 73rd and so on must be deleted (Not transferred to the next line) and then make a new line for the user to input more data. (I believe spaces count as a character space)

Also, let me mention that this program is suppose to take the users input, remove/replace all non-printing ASCII characters AND delete all non-ASCII characters, then after such changes are made, then we truncate the lines to 72 and print the result.

But for now, I just simply want to learn how to truncate the users input. I am working one step at a time. I have a feeling that I need some sort of if statement and counting trick inside the while loop to help me truncate this and create a new line, I just can't figure it out. any help? tips? thank you.

preguntado el 22 de septiembre de 13 a las 22:09

Declare an int variable, initially set to 0, that you increment in the loop to count columns. When you see a newline '\n', reset the count to 0. If the count is greater then 72, don't do the putchar(). -

It's easier to get your code right if you actually copy+paste it -- I doubt it worked with stdio en lugar de stdio.h and with no argument to putchar(). Solo digo. -

Also, move the "return 0;" line to the end of main, outside of the loop. -

@FatalError Not a good idea, I think. Typing and not getting it right is a symptom of needing more practice. Also, typing instead of pasting accelerates the learning process, in the long run. -

@MikeHousky: It also means everyone else wastes time debugging typos instead of the "Real" code ;). -

2 Respuestas

#include <stdio.h>
int main (int argc, char **argv)
  int a;
  int i = 0;
  while ( (a = getchar ()) != EOF) {
    if (++i < 73)
      putchar (a);
      if (i == 73)
        putchar ('\n');
    if (a == '\n')
      i = 0;
  return 0;

Respondido el 22 de Septiembre de 13 a las 23:09

Where are you incrementing i? - Martin

well, what is that whole fancy stuff in the parameters of main?Why are they there? how do they come into play? (As you can see i'm terribly lost in this.) - emperador511

@JesusGonzalez the arguments of main are useless for your program. I wrote them only down to show you how to do it right. You should consider reading this book: - ceving

The part that confuses me is how are "a" and "i" related? they just simply are? so you get a character with getchar and put it in a? and then magically it goes to i? - emperador511

The variable a there is the character read from the stdin input file. The variable i is the current column number. See how it's being reset to zero when a newline '\n' is seen. The only problem is that the test for the newline is in the wrong place for simplest code. - mike husky

Alternate version, more what I hoped you could come up with on your own. Try to see how this fits the description I gave above.

#include <stdio.h>
int main(void)
    int in_char;                 /* holds the next input character from stdin */
    int col_no = 0;              /* column number, initially zero */
    while ((in_char = getchar()) != EOF) /* read a character into in_char */
    {                                      /* (ends loop when EOF received) */

        /* Here is where to insert a test for a non-ASCII character */

        col_no = col_no + 1;     /* add 1 to the column number */
        if (in_char == '\n')     /* ...but reset to 0 if a newline is seen */
            col_no = 0;
        if (col_no <= 72)        /* output the character if not beyond col. 72 */
    return 0;

That was the general idea, only with some overcommenting added to explain the steps. Type this in without the comments, and try to understand the left side as doing the things described on the right.

Respondido el 23 de Septiembre de 13 a las 01:09

sorry for the millions of questions, but first how does "col_no" know it's going to be used for counting columns? Why doesnt "in_char" have the same effects, if I make another variable called "col_no2" is it going to instantly assume its being used to count columns? 2nd why exactly are we adding +1 to col_no? ok so going down the while loop, if its true we execute it, then we add 1 to col_no, it fails first if statement right? and then it goes to second if statement and you plug 1 into the col_no? and the if statement is true so we print? is that how that works? - emperador511

Your code merges every clipped line with its successor. For every clipping an extra newline must be written or the newline after the clipping column must be written. - ceving

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