how to remove zeros from int array? for ex if i have a array 1,2,0,34 than result is 1,2,34.
i was thinking may be use trim function?
another idea was to put space. for ex 1,0,2,0,3 than reslust is 1, ,2, ,3

note iam trying to do this without making another array.

int a[5];  //has the value 0,1,0,3

for(i = 0; i < 5; i++)
{
  if(a[j] == 0)
   a[i] = ' '?
}

one way is to store the values after the index containing 0 to an index before it, but you need to be aware of the last index of the array that has a value

A better approach would be using stack or queue

isnt there a function that does that?
is there a way to replace 0 with space?

s there a way to replace 0 with space?

you cannot replace 0 with a space, as you are using an integer array. Whereas, space comes under characters. If you try to do that, the program will print the ASCII value of space bar instead of giving a space

And when you are incrementing "i" , you need to take

if(a[i]==0)

how to remove zeros from int array? for ex if i have a array 1,2,0,34 than result is 1,2,34.

I wrote my program in this way... See how does it work.. wht i did is, whenever a zero occured in array, i swapped the value with the next one.

Here's my code!

#include<conio.h>
#include<stdio.h>
int main()
{
 int a[5],i;  //has the value 0,1,0,3
 printf("enter array elements");
 for(i=0;i<5;i++)
  {
    scanf("%d",&a[i]);
  }
 printf("Array elements are\n");
 for(i = 0; i < 5; i++)
 {
    if(a[i] == 0)
     {
       a[i]=a[++i];  //this is what i did
     }
    printf("%d ",a[i]);
 }
 return 0;
}

@Vish0203

nope you need to think about every scenario that could happen

what if the array has values:
1 2 0 4 0 , //values that end with zero or
1 0 0 4 5 //values that have successive or repeating zeros

etc....

so i guess the only way would be to make a two arrays?

@zeroliken
Hmmm.. you are right!! didn't thought about that!!

@hwoarang69 yea... you better go with it!!

hey!!! wait a sec!!

How about this???? ? The second "If" statement, checks for the repeated zeroes...

#include<conio.h>
#include<stdio.h>
int main()
{
 int a[5],i;  //has the value 0,1,0,3
 printf("enter array elements");
 for(i=0;i<5;i++)
  {
     scanf("%d",&a[i]);
  }
 printf("Array elements are\n");
 for(i = 0; i < 5; i++)
 {
     if(a[i] == 0)
      {
            //this is what i did
         a[i]=a[++i];
      }
    if(a[i]!=0)
  printf("%d ",a[i]);
 }
 return 0;
}

naah!! its wrong.. sorry!! We can't do it!!

I just threw this together. It will move all zero values to the end of the array. Hope it helps.

    int iarr[] = {1, 3, 0, 0, 5, 7, 0, 0, 0, 9, 9, 6, 0};
    int len = sizeof(iarr)/sizeof(iarr[0]);
    int i , j, idx;

    for (idx = 0; idx < len; ++idx)
    {
        if (iarr[idx] == 0)
        {
            j = idx;
            ++j;

            while (iarr[j] == 0 && j < len)
            {
                ++j;
            }

            iarr[idx] = iarr[j];
            // set substituted value to zero
            iarr[j] = 0;
        }
    }

    for (i = 0; i < len; ++i)
    {
        printf("%d ", iarr[i]); // 1, 3, 5 ,7, 9, 9, 6, 0, 0, etc
    }

yea.. that's ok but, we dont want zeroes to be displayed...

Then don't show them.

for (i = 0; i < len; ++i)
{
    if (! iarr[i])
        break;

    printf("%d ", iarr[i]); // 1 3 5 7 9 9 6
}

Also in my previous post, lines 10 to 15 can be replaced with:

while (iarr[++j] == 0 && j < len) ;

Your program shows a garbage value at the end!
i mean the output is

1 3 5 7 9 9 6 9151

Can you remove this garbage value?

To remove the garbage, put lines 17 to 19 of nullptr's previous post in an if statement with condition j < len

hehe.... Problem solved!!!

Simpler solution is to replace lines 10 to 15 with

while (iarr[++j] == 0 && j < len - 1) ;

thanks alot guys!

Are you sure that you needed to remove them instead of only not show them or pass them?

#include <stdio.h>
#include <stdlib.h>

int iarr[] = {1, 3, 0, 0, 5, 7, 0, 0, 0, 9, 9, 6, 0, 9};
int len = sizeof(iarr)/sizeof(iarr[0]);

int main()
{
    int i;
    for (i = 0; i < len; i++)
    {
        if(iarr[i]) printf("%d ", iarr[i]);
    }
}

@pytony

That's awesome!!
in "if" statement, if iarr[i]=0, means the statement is false and hence it doesn't print it!!
Is that what happening there???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.