0

How do i trim a char array ?

I'm reading this array from the text file and when it matches a certain string , ive to process it. But it never matches. I assume because it contains some trailing and leading spaces or\and '\n'

I dont know how to trim a char array but i used this to removes spaces. This has a problem that when token[0] = {0 0 0 0} this makes it {0000}. So i dont know what to do next..

char a[50] = {0};
int j = 0;

    //Removing spaces or garbage values in token[0] for perfect string match
    for(int i=0; (unsigned)i<strlen(token[0]) ; ++i )
    {
      if( (isalnum(token[0][i])) || token[0][i] == 95)
      {
        a[j]=token[0][i];
        j++;
      }
      else
      {
        continue;
      }
    }
7
  • 6
    Show us some code. What have you tried? Commented Apr 14, 2013 at 18:25
  • .. So write some code to take care of those eventualities. Commented Apr 14, 2013 at 18:27
  • Yes I'm trying to do that ! Commented Apr 14, 2013 at 18:42
  • @dharag - Why not post it and tell what is not working! Commented Apr 14, 2013 at 18:51
  • Where is the array token being populated from? Commented Apr 14, 2013 at 19:02

1 Answer 1

3

Given that you have not mentioned anything else, the following could always be done:

copy the array(to be checked) to another array char by char to only the number of chars required...(this way you could also check how many chars you need to trim):

a=0;

while(array1[a] != '\0'){
    array2[a] = array1[a];
    a++;
}

array2[a] = '\0';
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.