www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/01/18:28:03

From: Andrew Crabtree <andrewc AT typhoon DOT rose DOT hp DOT com>
Message-Id: <199704012317.AA047826661@typhoon.rose.hp.com>
Subject: Re: String comparing problem
To: deltaman AT swipnet DOT se
Date: Tue, 01 Apr 1997 15:17:41 PST
Cc: djgpp AT delorie DOT com
In-Reply-To: <33417135.1856@geocities.com>; from "Deltaman" at Apr 01, 97 10:33 pm

> In the beginner I set a string called 'test_string'
> 
> char test_string[5] ;
> #197; <--End of test string
> When I checks if test_string is equal to a special character:
> 
> if (test_string =3D=3D "#197; ") printf("I should see this");
> if (test_string =3D=3D "#197;")  printf("or this");

This program would IMHO be a perfect candidate for Flex.  I wrote a 
simple text to html scanner in seriously less than 3 minutes.

But - your problem seems to be that you are confusing BASIC with C.  
You cannot use the == operator on string values. Use strcmp instead

#include <string.h>

if (strcmp(test_string,"#197;")==0) printf("I should see this");



In Flex you could do something like this 


mylexer.l
----------------------------

%%

#197        { fprintf(yyout,"Special Value 1"); }
#198        { fprintf(yyout,"Special Value 2";  }

---------------------------------------------------

Thats it.  Add lines as needed for the codes you want to replace.

build it with 

flex mylexer.l
gcc -DYY_MAIN lexyy.c -o mylexer -lfl

run it like so 

mylexer < foo.htm > foo.txt 

(I did test this by the way)

Aside from being less code (no main routine, no manual compares,
no get character and append stuff) its most likely faster than
anything you can hand code.


Andrew








- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019