How to print out a specific numeric pattern

I just had a question about C. I have a file that has text in the format of:


I was wondering how I would read in the data and in another file, print out:


1 answer

  • answered 2018-09-21 18:19 Basile Starynkevitch

    Just consider these numbers as strings of characters (each character happens to be a digit). Then do character processing on them.

    For example:

    char str[16];
    memset (str, 0, sizeof(str));
    if (scanf(" %15[0-9]", &str) > 0) 
      printf("%.3s", str); 

    should, if fed with 0734327574, output 073 (notice that your example don't explain what should have happened in that case, and I am guessing one way of doing it. My guess could be wrong if 0734327574 is actually meant as an octal number for the decimal number 124891004).

    The rest is an exercise to the reader. Of course you need to carefully read the documentation of memset, of scanf, of printf. Don't forget to end printf format strings with \n or to call fflush on stdout (which is often line-buffered).

    Remember that numbers don't have digits. Only their notation have digits. The number 20, written in Roman notation XX, in English twenty, in French vingt, in hexadecimal 0x14, in octal 024 (or even 248), in binary 10100, as the simple arithmetic expression 3*7-1, is still the same number (which happens to be twice the number of fingers I have on my hands, and is also the number of arrondissements in Paris).