Question:

How can i use sprintf to keep track of how many characters have been printed on a line in c language

by  |  earlier

0 LIKES UnLike

I need to create a program that will count down from a specified number, using decrements of another specified number, however, to keep things neat, no more than 10 numbers can be printed on a line, or 70 characters. I have been given the hint to use sprintf to print the number to a string and then find the length of the formatted string. I have worked out the 10 numbers per line thing, however I am not really sure how to use sprintf to keep track of how many characters have been printed on the line, and I haven't been able to find any examples of the same sort of thing using the same function.

Any help is greatly appreciated :).

 Tags:

   Report

2 ANSWERS


  1. Another hint I suppose - you know how you can find out the length of that string with strlen... and compare that to your line limit of 70...

    something like:

    ...

    if(strlen(numberstring)>70)

    {

       cprintf("\r\n");  /* newline */

       strcpy(numberstring,NULL);  /* reset numberstring to "" */

    }

    else

    ...

    Hopefully you get it now... :)


  2. Once you sprintf the string, you can count how many characters are in it:

    char * currChar = stringResult[0];

    int noOfChar

    while(*currChar != '\0')

    {

    noOfChar++; //Add one to the number of good characters in the string

    currChar++; //Advance the character pointer to the next character in the array

    }

    <sarcasm target = "Moxie1313">

    Library functions that do tasks like these are clearly cheating! :-P

    </sarcasm>

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.