Re: Pascal - C (2)



Ian Collins said:

Ruud wrote:
Hallo allemaal,


During the conversion of my program from Pascal to C, I was more or
less able to find the C equivalent of most Pascal functions so far.
Only four gave me some real trouble. I solved them but it could be I
overlooked something.

1) In Pascal you can declare functions inside a function. AFAIK this
is not possible with C. Or am I wrong?

No, C does not have nested functions, although some compilers support
them as extensions.

Right so far.


2) In Pascal there exists the "in" function. Example:

if (c in ['A'..'F', '0'..'9']) then { c is hexadecimal }

This can be translated like:

if ( ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9'))) .... // c is hexadecimal

I just wonder if there is a more simpler solution.

Sorry, no.

Why not at least tell him about:

if(isupper(c) || isdigit(c))

and

if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c) != NULL)

neither of which is a precise match, but each of which might be used in
similar situations.


One alternative is to use a regular expression library if
you have a lot of these.

That seems like overkill to me.


3) In Pascal I can "add" lines:

Line1 = 'File size:' + sSize + ' bytes.';

My solution:

strcpy(Line1, "File size:");
strcat(Line1, sSize);
strcat(Line1, " bytes.);

Again, I just wonder if there is a more simpler solution.

Not in C.

Um, of course there is.

sprintf(Line1, "File size: %d byte%s", sSize, bytes == 1 ? "" : "s");

Note the extra flexibility that C gives you.


4) In Pascal I can "add" just one character of another string:

Str1 = Str2 + Str3[5];

Unfortunately strcat(Str1, Str3[5]); doesn't work, I get an error
message. My solution:

Str4[0] = Str3[5];
Str4[1] = 0;
strcpy(Str1, Str2};
strcat(Str1, Str4};

It works but in this case I'm certainly not happy with the solution.
Is there a better way?

Yes - sprintf can do this easily enough.

sprintf(Str1, "%s%c", Str2, Str3[5]);


Don't use C.

Um, he asked for a better way to do this in C. Not using C doesn't count.

C does not have string objects,

True, but it has a string format which works just fine.

of arrays of char and library functions to manipulate then.

I'm not sure what you mean here (unless "of" is a typo for "just" or "only"
or "merely", but yes, C has arrays of char and library functions to
manipulate them. Good!

If you are doing a lot of string manipulation, C might not be your best
choice. Scripting language like Perl are designed for string processing
and might be a better option.

ROTFL! If you are doing a shedload of string manipulation, C is a far
better choice than Perl. That is, if you want it to finish some time.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: Math
    ... floating point to string is performed by the C compiler, ... following instructions from perl. ... The conversion should not be too slow. ... and found that using this custom function would ...
    (comp.lang.perl.misc)
  • Re: Pascal - C (2)
    ... Ruud wrote: ... During the conversion of my program from Pascal to C, ... If you are doing a lot of string manipulation, C might not be your best ...
    (comp.lang.c)
  • Re: FAQ 4.3 Why isnt my octal data interpreted correctly?
    ... Perl only understands octal and hex numbers as such when they occur as ... they are read in from somewhere and assigned, no automatic conversion ... conversion to and from decimal strings). ... if you want to get the numeric value of a a string with an octal ...
    (comp.lang.perl.misc)
  • Re: FAQ 4.3 Why isnt my octal data interpreted correctly?
    ... perl must start with a leading 0 and hexadecimal literals ... no automatic conversion takes place. ... No. Perl treats *strings* as decimal when used in a numeric context. ... When a number is used in a string context, ...
    (comp.lang.perl.misc)
  • Re: numbers and strings and regex?
    ... Siegel) wrote: ... 0+$1 forces a conversion from the string to a number. ... Perl will convert as much of $1 as it can make sense as a number. ...
    (comp.lang.perl.misc)