Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #10694
| Newsgroups | comp.programming |
|---|---|
| Date | 2020-02-27 08:06 -0800 |
| References | <0b8d1b58-3651-47ed-a2b8-63dc1d659c91@googlegroups.com> <87y2souis2.fsf@bsb.me.uk> |
| Message-ID | <bcc61784-004c-4bf9-bc09-fa44d22f83c8@googlegroups.com> (permalink) |
| Subject | Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) |
| From | Julio Di Egidio <julio@diegidio.name> |
On Thursday, 27 February 2020 16:55:29 UTC+1, Ben Bacarisse wrote:
> "Chad Altenburg, Ph.D." <cdalten@gmail.com> writes:
<snip>
> > My gut feeling says this isn't right. But I have no idea how this
> > could be done in one loop.
>
> Here's a C solution. It's messy because the natural way to do string
> things in C is to pass an array to take the result. You then need to
> pass a size so the function does not write out-of-bounds. But to be
> useful the function should keep going and return the size that's needed
> even when there was no room this time:
>
> #include <stdio.h>
>
> size_t zipzap(const char *src, char *dst, size_t n)
> {
> size_t i = 0;
> while (*src) {
> if (i < n)
> dst[i] = *src;
> i += 1;
> if (src[0] == 'z' && src[1] && src[2] == 'p') {
> if (i < n)
> dst[i] = 'p';
> i += 1;
> src += 3;
> }
> else src += 1;
> }
> dst[i <= n ? i : n-1] = 0;
> return i + 1;
> }
>
> int main(int argc, char **argv)
> {
> char z[10];
> for (int a = 1; a < argc; a++) {
> size_t required = zipzap(argv[a], z, sizeof z);
> printf("%s -> %s (%zu)\n", argv[a], z, required);
> }
> }
>
> The more "scripty" way -- allocating the result -- looks simpler:
>
> char *zipzap_str(const char *src)
> {
> char result[strlen(src) + 1], *dst = result;
> while (*src) {
> *dst++ = *src++;
> if (src[-1] == 'z' && src[0] && src[1] == 'p') {
> *dst++ = 'p';
> src += 2;
> }
> }
> *dst++ = 0;
> char *rp = malloc(dst - result);
> return rp ? strcpy(rp, result) : 0;
> }
Man, that hurts...
Julio
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Coding while drunk and stoned take 2. (Yes, I know this is a cross post) "Chad Altenburg, Ph.D." <cdalten@gmail.com> - 2020-02-27 06:04 -0800
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) Julio Di Egidio <julio@diegidio.name> - 2020-02-27 07:24 -0800
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 15:55 +0000
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) Julio Di Egidio <julio@diegidio.name> - 2020-02-27 08:06 -0800
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 21:00 +0000
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) "Chad Altenburg, Ph.D." <cdalten@gmail.com> - 2020-02-27 15:18 -0800
Re: Coding while drunk and stoned take 2. (Yes, I know this is a cross post) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 21:06 +0000
csiph-web