Sunday, October 30, 2022

[FIXED] How do i "delete" useless , after scanf()

Issue

Here is a piece of my code : And output looks like: 12, 44, 55, I need to remove the last one ", " and i tried everything.

while ((r = scanf("%d", &v)) > 0){
        printf("%d", v);
        printf(", ");
        }

Solution

Instead of printing the comma after each value, print it before each value except the first:

int first = 1;
while ((r = scanf("%d", &v)) > 0){
    if (!first) printf(", ");
    first = 0;
    printf("%d", v);
}


Answered By - dbush
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.