0

I have an array with some values like:

char s[] = {" ","a","b","c"," "}; 

How I can remove these empty values?

Description (Collected from comment):

I have an array that I got from a function and when i printed with sprintf returns me a string like this: " foo" two blanks or depend, this because I am replacing some characters with blank-space (' ') characters, but when I use the variable in another function, these spaces gives me errors because the empty values, then I need to do a "split" like java does, how can I do this?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
maudev
  • 974
  • 1
  • 14
  • 32
  • 1
    This declaration/definition is wrong, enable compilation warnings, what does your compiler tell you? Also please clarify what do you mean by *remove*? It's not the way a c programmer would think! So please explain why you want to do this to provice a c ish solution. Note that "*removing*" the values would in general be unnecessary work. – Iharob Al Asimi Oct 09 '15 at 03:19
  • I have an array that I got from a function and when i printed with sprintf returns me a string like this: " foo" two blanks or depend, this because I am replacing some characters with blankspace (' ') characters, but when I use the variable in another function, these spaces gives me errors because the empty values, then I need to do a "split" like java does, how can I do this? – maudev Oct 09 '15 at 03:26
  • you can create another array of string and save only valid strings in that. If the string is empty, don't copy to the newer array. – Pawan Oct 09 '15 at 03:44
  • @user3295036 Over-write your original array . Using a function or using loop. – ameyCU Oct 09 '15 at 03:59
  • http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way take a look at this question. If u want to split the string u can use strtok function in C. – user2979190 Oct 09 '15 at 05:52
  • OP, create your own answer and accept it. Do not put your answer in the post – chux - Reinstate Monica Oct 09 '15 at 13:56

1 Answers1

-1

Not sure about "deleting them", but you can count the everything except for the "blank spaces", and create an array of that size and transfer all the "non-blank spaces".

Something like this maybe? This may not be the best answer out there, this is just for demonstration purposes.

I don't know if you will have an end of line character in your char array... so this is another way of doing it.

#include <stdio.h>

int count (char s[5]){
    int count = 0, i;
    for(i=0; i<5;i++){
        if(s[i]!=' ')count++;
    }
    return count;
}

void load(int size, char s[size], char s1[5]){
    int i, j=0;
    for(i=0; i<5; i++){
    if( s1[i]!=' '){s[j]=s1[i];
    j++;}
    }
}

int main(){

char s[5] = {' ','a','b','c',' '};
int size = count(s);
char newS[size];
load(size, newS, s);
puts(newS);

return 1;}
LearningCODE
  • 165
  • 1
  • 2
  • 13
  • Ok this works fine, but the size remains the same as the other array, i need to pass it to the new array with the new size of valid values, therefore I said how I can 'remove' those values – maudev Oct 09 '15 at 04:59
  • @user3295036 The function counts the valid char values in the string... Then returns that value to make an array of that size, so, no the new array has a different size (in the case above, it would be of size 3 instead of size 5). – LearningCODE Oct 09 '15 at 17:42