1

is there a way to print out the line number in C while only using putchar and getchar and no arrays?

the output should look like this. for example input mink

01: mink
02: jaguar 

and so on

The line number should go from 01 to 50.

This is my start approach

#include <stdio.h>

int main() {
    int c;
    int counter = 1;

    while ((c = getchar()) != EOF) {
        putchar(c);
        if (c == '\n') {
            putchar(counter + '0');
            putchar(':');
            putchar(' ');
            ++counter;
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189

2 Answers2

0

Assuming the line number is only 2 digits, compute and output these digits and the : prefix. To avoid duplicating code for the beginning of the file and to avoid printing a line number for the non-existing line after the end of the file, print the line number before the first character of each line, that is after the newline if there is another character. Initializing lastc to '\n' ensures a line number is printed before the first line of the file, if there is at least one line.

Here is a simple imlementation:

#include <stdio.h>

int main() {
    int c, lastc = '\n';
    int counter = 1;

    while ((c = getchar()) != EOF) {
        if (lastc == '\n') {
            putchar('0' + counter / 10 % 10); // tenths digit
            putchar('0' + counter % 10);      // units digit
            putchar(':');
            putchar(' ');
            counter++;
        }
        putchar(c);
        lastc = c;
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thx for your comment but I am not allowed to use print f –  May 08 '18 at 14:36
  • Sorry, I overlooked the fact that you cannot use `printf` to output the line number. If you can assume the line number to be between `1` and `99`, it is rather simple: at the beginning of each line, compute and output the 2 digits and the `: `. – chqrlie May 08 '18 at 18:52
  • Thank you very much :) sorry english is not my first language but what exactly do you mean by "at the beginning of each line, compute and output the 2 digits and the : " I have trouble understanding and what does the lastc = c; do? –  May 08 '18 at 19:37
  • I want the program to print the line numbers from 01 to 99 after that it should print out the output without line numbers what approach should I take? –  May 08 '18 at 21:15
  • Add a test to only output the line number if `counter < 100` – chqrlie May 08 '18 at 21:21
  • Thank you sooo much –  May 09 '18 at 08:42
  • sorry one more question I don't really understand the if(lastc=='\n') why lastc and not c? I understand that if I say if(c=='\n') it will print the output before the line number. But how does c=getchar know that last c is my last character? –  May 09 '18 at 08:57
  • so my main question how does getchar know that lastc ist my last character? –  May 09 '18 at 08:57
  • 1
    `lastc` is a separate variable to store the previous character read from the file. Maybe I should have named it `prevc`... I initialize it to `'\n'` to ensure that the line number `01` is printed before the first line of output, and I store the value of `c` into it at the end of the loop, so it is always the value of the previous character when I call `getchar` again. – chqrlie May 09 '18 at 19:12
0

You need to do division and mod by 10 to get the 10's digit and the one's digit.

#include <stdio.h>
int main()
{
    int c;
    int counter=1;
    putchar('0');
    putchar('1');
    putchar(':');
    putchar(' ');
    while((c=getchar())!=EOF){
        putchar(c);
        if ( c == '\n' ) {
            ++counter;
            putchar(counter / 10 + '0');
            putchar(counter % 10 + '0');
            putchar(':');
            putchar(' ');

        }
    }
    return 0;
}
Zach Jones
  • 46
  • 4
  • thx for your answer but if the input is for example box then the output looks like this 01: newline –  May 08 '18 at 14:37
  • how can I modify the code that the output looks like this 01: box –  May 08 '18 at 14:37
  • I also want the line numbering to go from 01: to 50: do i need a do while loop for that? –  May 08 '18 at 14:46
  • @momonosuke See my edits. You could do a do-while, but then the also have to do c = getChar() outside of the loop – Zach Jones May 08 '18 at 15:37
  • I tried putting the c = getchar() outside the main while loop and after the do while loop however I still get an endless loop –  May 08 '18 at 18:05