delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/02/01:28:07

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: String comparing problem
Date: Tue, 01 Apr 1997 07:11:53 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 49
Message-ID: <334125B9.6FCD@cs.com>
References: <33417135 DOT 1856 AT geocities DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp203.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Deltaman wrote:
> =

> I have a program to exchange special HTML-letters to plain text i.e
> &#233 to =E9=82 and so on.
> =

> In the beginner I set a string called 'test_string'
> =

> char test_string[5] ;

This is too short.  This allocates space for five characters, but you
must also account for the null character ('\0') that terminates all
strings in C.  "#197;" is SIX characters, not five.  "#197; " is seven
characters.  Consider yourself lucky if such bugs don't crash your
programs.

> 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 is not how strings are compared in C.  You must use the strcmp()
library function, like so:

if ( strcmp( test_string, "#197; " ) =3D=3D 0 ) printf( "I should see thi=
s"
);
if ( strcmp( test_string, "#197;" =3D=3D 0 ) printf( "or this" );

These are all extremely basic questions about the C language itself. =

While we are happy to help you out here, this newsgroup is primarily a
forum for djgpp-specific questions, not basic programming questions.  I
recommend that you curl up with a good C book, such as The Waite Group's
_New C Primer Plus_, or the Kernighan and Ritchie book whose name I can
never remember.  ;)  C is not Pascal, nor is it BASIC, and making such
assumptions can get you into lots of trouble.

-- =

---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
| "Starting flamewars since 1993"  |   http://www.cs.com/fighteer   |
|  *** NOTICE ***  This .signature is generated randomly.           |
|                  If you don't like it, sue my computer.           |
---------------------------------------------------------------------

- Raw text -


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