Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By Salem

Parsing a string

This is a discussion on Parsing a string within the C Programming forums, part of the General Programming Boards category; i am writing a tokenizer to parse a string. e.g"T T && F && !".Every character is separated by space.I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Parsing a string

    i am writing a tokenizer to parse a string. e.g"T T && F && !".Every character is separated by space.I have set up my tokeniser to return T,T,&,F,&,!.The problem that i have is that its returning a space at the last character which i do not know where it is coming from

    Code:
    }
    void parseexpressionfromcommandline(char * unparsedexpression,char *parsedexpression){
         
        if(unparsedexpression==NULL )
            printf("does not point to any address");
        else {
            char value;  //this would hold my value 
            int pos=0;
            
           
            while(*unparsedexpression!='\0'){
              
                while(!isspace(*unparsedexpression)&& (*unparsedexpression)!='\0'){
                 value=(*unparsedexpression++);
               
                }//inneer while loop
            
            unparsedexpression++;
            parsedexpression[pos++]=value;
          
            }//outer while loop
            
            printf("%s \n",parsedexpression);
            printf("%u",strlen( parsedexpression));
             
        }//outer while loop
        
        
    }
    Last edited by sigur47; 05-06-2012 at 04:20 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > if(unparsedexpression==NULL )
    You need to do this before doing anything else - like for example using strlen()

    > parsedexpression[pos++]=value;
    Chances are, your parsed expression is an uninitialise string.

    You need to do
    parsedexpression[pos]='\0';
    before you try to print it, or use strlen() on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Salem View Post
    > if(unparsedexpression==NULL )
    You need to do this before doing anything else - like for example using strlen()

    > parsedexpression[pos++]=value;
    Chances are, your parsed expression is an uninitialise string.

    You need to do
    parsedexpression[pos]='\0';
    before you try to print it, or use strlen() on it.
    I am confused.Does the printing add extra space to the parsed string.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Your code never terminates parsedexpression with a trailing '\0'. printf's %s format and strlen() are both looking for that terminator .... which may not be there at all.

    Wouldn't it be easier to simply remove the spaces from unparsedexpression?
    sigur47 likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    If you don't add a \0 (or pre-fill the parsed string with \0), then you'll print whatever garbage was there up to wherever a \0 happens to be present.

    > while(!isspace(*unparsedexpression)&& (*unparsedexpression)!='\0')
    But then again, this is skipping ALL non-whitespace chars and then storing a space.

    Perhaps you should be storing chars INSIDE this loop?
    sigur47 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing a string
    By bhdavis1978 in forum C Programming
    Replies: 3
    Last Post: 11-04-2010, 02:51 PM
  2. parsing a string
    By Coding in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2008, 05:01 AM
  3. Parsing a String
    By mas0nite in forum C++ Programming
    Replies: 15
    Last Post: 10-27-2006, 09:35 AM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. Parsing a String... ??
    By leeor_net in forum C Programming
    Replies: 2
    Last Post: 07-04-2004, 10:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21