Home > Back-end >  Remove the last comma from c loop
Remove the last comma from c loop

Time:01-13

i have the belowo loop in c that print the prime number

  for(int i = 2; i<=arraySize; i  )
    {
        //If arraySize is not 0 then it is prime
       if (numbers[i]!=0)
        printf("%d,",numbers[i]);
      
    }

the out put after enter 50 for example is 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47, i want to to not print the last comma how i can do it

i tried this code but not working

printf("%d%s", numbers[i], (i !=arraySize-1) ? "," : "");

CodePudding user response:

Instead of printing a comma after each number, print it before. Then you can use a variable to tell if this is the first number being printed, and not print the comma.

first=true;

for(int i = 2; i<=arraySize; i  )
{
    //If numbers[i] is not 0 then it is prime
    if (numbers[i]!=0) {
        printf("%s%d", (first ? "" : ","), numbers[i]);
        first = false;
    }  
}

CodePudding user response:

I like both the other answers but just want to throw in this error prone variant on the same theme.

_Bool first = true;

for (int i = 2; i <= arraySize; i  ) {
    if (numbers[i] != 0) {
        printf(",%d"   first, numbers[i]);
        first = false;
    }
}

If first is true the actual formatting string will become "%d". If it's false it'll become ",%d".

CodePudding user response:

Simple: Use a pointer to the "prefix" string, printed AHEAD of the next value:

char *sep = "";
for(int i = 2; i <= arraySize; i   ) {
    if( numbers[i] ) {
        printf( "%s%d", sep, numbers[i] );
        sep = ", "; // I added a SP, too
    }
}

Here's an alternative that uses a "limited scope" variable to index a static string. For clarity in this example, the array boundaries have been adjusted.

int main( void ) {
    int numbers[] = { 1, 1, 4, 8, 9, 0, 7 };
    int arraySize = sizeof numbers/sizeof numbers[0];

    for( int i = 0, out = 0; i < arraySize; i   )
        if( numbers[i] )
            printf("%s%d", &","[!out  ], numbers[i] );

    return 0;
}

The negated boolean post-incrementing value of out provides the address of the '\0' to the first instance, then the address of "," in subsequent instances.

1,1,4,8,9,7

CodePudding user response:

The answers already here are fine, but I'd like to add a "simpler" solution. Simpler in that it doesn't require any further logic or extra variables. It does, however, require that you know that the first number is non-zero.

printf("%d", numbers[2]);
for (int i = 3; i < arraySize; i  )
{
    if (numbers[i] != 0)
        printf(",%d", numbers[i]);
}

CodePudding user response:

I think the other answers overcomplicates things. I don't see any reason to have a test for every iteration in the loop. Instead, I'd simply do the special case first:

printf("%d", numbers[2]);

for (int i = 3; i <= arraySize; i  ) {
    if (numbers[i]!=0) 
        printf(",%d", numbers[i]);
}

This will however need some additional code to correctly handle the case where arraySize is lower than 3.

But I would choose another approach from the beginning, and that is writing a good function for printing an array. Could look like this:

void printArray(const int *array, int size) {
    putchar('['); // Of course this is optional

    if(size > 0) {
        printf("%d", array[0]);

        for(int i=0; i<size; i  )
            printf(",%d", array[0]);
    }

    putchar(']'); // And this too
}

and then something like this:

int convertArray(const int *numbers, int *array, int size) {
    int ret = 0;

    for(int i=0; i<size; i  ) {
        if(number[i] != 0) {
            array[ret] = numbers[i];
            ret  ;
        }
    }

    return ret;
}
  •  Tags:  
  • c
  • Related