Re: Pascal - C (2)
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Sun, 02 Nov 2008 06:54:51 +0000
Ian Collins said:
Ruud wrote:
Hallo allemaal,No, C does not have nested functions, although some compilers support
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?
them as extensions.
Right so far.
2) In Pascal there exists the "in" function. Example:Sorry, no.
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.
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:Not in C.
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.
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
.
- Follow-Ups:
- Re: Pascal - C (2)
- From: George
- Re: Pascal - C (2)
- From: Keith Thompson
- Re: Pascal - C (2)
- From: Ian Collins
- Re: Pascal - C (2)
- References:
- Pascal - C (2)
- From: Ruud
- Re: Pascal - C (2)
- From: Ian Collins
- Pascal - C (2)
- Prev by Date: Re: The wrong with factorial function
- Next by Date: Re: The wrong with factorial function
- Previous by thread: Re: Pascal - C (2)
- Next by thread: Re: Pascal - C (2)
- Index(es):
Relevant Pages
|