Velocity Reviews > calculate value between two strings

calculate value between two strings

Ben Bacarisse
Guest
Posts: n/a

 10-26-2010
Chad <(E-Mail Removed)> writes:

> On Oct 23, 2:19Â*pm, Eric Sosman <(E-Mail Removed)> wrote:
>> On 10/23/2010 4:51 PM, John Green wrote:

<snip>
>> > I would like to calculate the value between two strings. Â*For example:
>> > abc-aaa would be 29.

<snip>
>> Â* Â* Â*3) If the strings are of unequal length, you could pretend that the
>> Â* Â* Â* Â* shorter had a prefix of "aaa...aaa" to extend it to the length
>> Â* Â* Â* Â* of the longer. Â*In this case it might be easier to work from
>> Â* Â* Â* Â* right to left and accumulate delta a little differently.

>
> Bear with this. I have enough alcohol in my system right now that if I
> would get pulled over for a DUI, the thingy the California cops use to
> test how much alcohol is on your breathe would probably be three times
> the legal limit.

http://xkcd.com/323/

> With that, what strategies would you use to attack
> case 3?
>
> Uhh... cause like, here is what I came up with so far....
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> char calculate(char first[], char second[])
> {
> char delta = 0;

This is unlikely to be big enough.

> char difference = 0;
>
> for( ;*first && *second; *first++,*second++) {

The *s in the third clause are not doing any good.

> difference = *first - *second;
> delta = delta * 26 + difference;
> }
>
> return delta;
> }

But the main point is that the above seems to have nothing much to do
with "case 3" where the strings are of different lengths.

<snip>
--
Ben.

Barry Schwarz
Guest
Posts: n/a

 10-26-2010
On Mon, 25 Oct 2010 17:50:29 -0700 (PDT), Chad <(E-Mail Removed)>
wrote:

snip

>
>Uhh... cause like, here is what I came up with so far....
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>char calculate(char first[], char second[])

Do you really expect the calculated value to fit in a char?

>{
> char delta = 0;
> char difference = 0;
>
> for( ;*first && *second; *first++,*second++) {

*first++ is equivalent to *(first++). What purpose do you think the *
serves?

> difference = *first - *second;
> delta = delta * 26 + difference;
> }
>
> return delta;
>}
>
>int main(void)
>{
> char first[BUFSIZ];
> char second[BUFSIZ];
>
> printf("Enter the first string: ");
> scanf("%s", first);
> fflush(stdout);

This is one statement too late.

> printf("Enter the second string: ");
> scanf("%s", second);
> fflush(stdout);
>
> printf("The difference between %s - %s is %d\n", first, second, \
> calculate(first, second));
>
> return 0;
>}

--
Remove del for email

Guest
Posts: n/a

 10-26-2010
On Oct 25, 6:37*pm, Barry Schwarz <(E-Mail Removed)> wrote:
> On Mon, 25 Oct 2010 17:50:29 -0700 (PDT), Chad <(E-Mail Removed)>
> wrote:
>
> snip
>
>
>
> >Uhh... cause like, here is what I came up with so far....

>
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include <string.h>

>
> >char calculate(char first[], char second[])

>
> Do you really expect the calculated value to fit in a char?
>
> >{
> > *char delta = 0;
> > *char difference = 0;

>
> > *for( ;*first && *second; *first++,*second++) {

>
> *first++ is equivalent to *(first++). *What purpose do you think the *
> serves?

To get what first (and second) is pointing at?

If I redo the for statement as

for( ;*first && *second; first++,second++) {
difference = *first - *second;
delta = delta * 26 + difference;
}

The warnings go away. I tried to think about it. But quite frankly, I
have so much alcohol in my system right now that I have enough issue
typing. I mean, can you only imagine what would happen if I would stop
and think about why the latter construction actually doesn't produce
any warnings?!
>
> > * *difference = *first - *second;
> > * *delta *= delta * 26 + difference;
> > *}

>
> > *return delta;
> >}

>
> >int main(void)
> >{
> > *char first[BUFSIZ];
> > *char second[BUFSIZ];

>
> > *printf("Enter the first string: ");
> > *scanf("%s", first);
> > *fflush(stdout);

>
> This is one statement too late.

Does the order matter in this case?

Guest
Posts: n/a

 10-26-2010
On Oct 25, 6:37*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Chad <(E-Mail Removed)> writes:
> > On Oct 23, 2:19*pm, Eric Sosman <(E-Mail Removed)> wrote:
> >> On 10/23/2010 4:51 PM, John Green wrote:

> <snip>
> >> > I would like to calculate the value between two strings. *For example:
> >> > abc-aaa would be 29.

> <snip>
> >> * * *3) If the strings are of unequal length, you could pretend that the
> >> * * * * shorter had a prefix of "aaa...aaa" to extend it to the length
> >> * * * * of the longer. *In this case it might be easier to work from
> >> * * * * right to left and accumulate delta a little differently.

>
> > Bear with this. I have enough alcohol in my system right now that if I
> > would get pulled over for a DUI, the thingy the California cops use to
> > test how much alcohol is on your breathe would probably be three times
> > the legal limit.

>
> http://xkcd.com/323/
>
> > With that, what strategies would you use to attack
> > case 3?

>
> > Uhh... cause like, here is what I came up with so far....

>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>

>
> > char calculate(char first[], char second[])
> > {
> > * char delta = 0;

>
> This is unlikely to be big enough.
>
> > * char difference = 0;

>
> > * for( ;*first && *second; *first++,*second++) {

>
> The *s in the third clause are not doing any good.
>
> > * * difference = *first - *second;
> > * * delta *= delta * 26 + difference;
> > * }

>
> > * return delta;
> > }

>
> But the main point is that the above seems to have nothing much to do
> with "case 3" where the strings are of different lengths.
>

I was doing case 1. Then, right as I finished case 1, I realized that
I had no idea on how to attack case 3. So instead, I posted my
attempted solution at case one. Now I would like to point out that I
should get a passing grade since..

a)I was under the influence at the time I wrote this code. Wait, I
still am.
b)I crank this code out in less than 30 minutes. And this was done
without the aid of google.
c)I don't work as a computer programmer for a living.

Barry Schwarz
Guest
Posts: n/a

 10-26-2010
On Mon, 25 Oct 2010 18:55:06 -0700 (PDT), Chad <(E-Mail Removed)>
wrote:

>On Oct 25, 6:37*pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On Mon, 25 Oct 2010 17:50:29 -0700 (PDT), Chad <(E-Mail Removed)>
>> wrote:
>>
>> snip
>>
>>
>>
>> >Uhh... cause like, here is what I came up with so far....

>>
>> >#include <stdio.h>
>> >#include <stdlib.h>
>> >#include <string.h>

>>
>> >char calculate(char first[], char second[])

>>
>> Do you really expect the calculated value to fit in a char?
>>
>> >{
>> > *char delta = 0;
>> > *char difference = 0;

>>
>> > *for( ;*first && *second; *first++,*second++) {

>>
>> *first++ is equivalent to *(first++). *What purpose do you think the *
>> serves?

>
>To get what first (and second) is pointing at?

But you never use the dereferenced value. That is what the diagnostic
from your compiler was trying to tell you. The only useful effect of
these two expressions is the side effect of increasing the pointers.

>
>If I redo the for statement as
>
>for( ;*first && *second; first++,second++) {
> difference = *first - *second;
> delta = delta * 26 + difference;
>}
>
>The warnings go away. I tried to think about it. But quite frankly, I
>have so much alcohol in my system right now that I have enough issue
>typing. I mean, can you only imagine what would happen if I would stop
>and think about why the latter construction actually doesn't produce
>any warnings?!

What you are saying is your opinions are irrelevant. This discussion
is for the benefit of the OP who might otherwise be tempted to follow

>>
>> > * *difference = *first - *second;
>> > * *delta *= delta * 26 + difference;
>> > *}

>>
>> > *return delta;
>> >}

>>
>> >int main(void)
>> >{
>> > *char first[BUFSIZ];
>> > *char second[BUFSIZ];

>>
>> > *printf("Enter the first string: ");
>> > *scanf("%s", first);
>> > *fflush(stdout);

>>
>> This is one statement too late.

>
>
>Does the order matter in this case?

Of course it does. On some systems, your printf output will not be
visible until the fflush executes. On those systems, your user will
not know why the system has stopped.

--
Remove del for email

Guest
Posts: n/a

 10-26-2010
> Of course it does. *On some systems, your printf output will not be
> visible until the fflush executes. *On those systems, your user will
> not know why the system has stopped.
>
> --
> Remove del for email

I don't know if the OP is still reading this, but how about something
like the following (for case 1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void calculate(char *value, char first[], char second[])
{
char difference = 0;
char delta = 0;

for( ;*first && *second; first++,second++) {
difference = *first - *second;
delta = delta * 26 + difference;
}
*value = delta;
}

int main(void)
{
char first[BUFSIZ];
char second[BUFSIZ];
size_t f_len, s_len;
char value;

printf("Enter the first string: ");
fflush(stdout);
if (scanf("%s", first) == 1) {
f_len = strlen(first) + 1;
} else {
fprintf(stderr, "Try again\n");
}

printf("Enter the second string: ");
fflush(stdout);
if (scanf("%s", second) == 1) {
s_len = strlen(second) + 1;
} else {
fprintf(stderr, "Try again\n");
}

if (f_len == s_len) {
calculate(&value, first, second);
printf("The difference between %s - %s is %d\n", first, second,
value);
} else {
fprintf(stderr, "Lengths don't match\n");
}

return 0;
}

Keith Thompson
Guest
Posts: n/a

 10-27-2010
Chad <(E-Mail Removed)> writes:
>> Of course it does. Â*On some systems, your printf output will not be
>> visible until the fflush executes. Â*On those systems, your user will
>> not know why the system has stopped.

>
> I don't know if the OP is still reading this, but how about something
> like the following (for case 1)
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> void calculate(char *value, char first[], char second[])
> {
> char difference = 0;
> char delta = 0;
>
> for( ;*first && *second; first++,second++) {
> difference = *first - *second;
> delta = delta * 26 + difference;
> }
> *value = delta;
> }

Wouldn't it be more convenient to have the function return the result
rather than assigning it via a pointer parameter?

The OP never bothered to tell us what "difference" means. Actually, he
didn't even use the word "difference"; he said "the value between two
strings".

> int main(void)
> {
> char first[BUFSIZ];
> char second[BUFSIZ];

Why would BUFSIZ (the size of the buffer used by the setbuf function)
be a reasonable size for an array used for interactive input? (It's
8192 on my system.)

> size_t f_len, s_len;
> char value;
>
> printf("Enter the first string: ");
> fflush(stdout);
> if (scanf("%s", first) == 1) {
> f_len = strlen(first) + 1;
> } else {
> fprintf(stderr, "Try again\n");
> }

scanf() with a "%s" format is as dangerous as gets(). It's also odd
to ask for a string, but then read a whitespace-delimited word.

> printf("Enter the second string: ");
> fflush(stdout);
> if (scanf("%s", second) == 1) {
> s_len = strlen(second) + 1;
> } else {
> fprintf(stderr, "Try again\n");
> }
>
> if (f_len == s_len) {

The only thing you do with f_len and s_len is compare them here.
You *could* just write
if (strlen(first) == strlen(second) ...

That's not to say that storing the lengths in variables is a bad thing
-- but why did you add 1 to them?

> calculate(&value, first, second);
> printf("The difference between %s - %s is %d\n", first, second,
> value);
> } else {
> fprintf(stderr, "Lengths don't match\n");
> }
>
> return 0;
> }

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Guest
Posts: n/a

 10-27-2010
On Oct 26, 7:08*pm, Keith Thompson <(E-Mail Removed)> wrote:
> Chad <(E-Mail Removed)> writes:
> >> Of course it does. *On some systems, your printf output will not be
> >> visible until the fflush executes. *On those systems, your user will
> >> not know why the system has stopped.

>
> > I don't know if the OP is still reading this, but how about something
> > like the following (for case 1)

>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>

>
> > void calculate(char *value, char first[], char second[])
> > {
> > * char difference = 0;
> > * char delta = 0;

>
> > * for( ;*first && *second; first++,second++) {
> > * * difference = *first - *second;
> > * * delta *= delta * 26 + difference;
> > * }
> > * *value = delta;
> > }

>
> Wouldn't it be more convenient to have the function return the result
> rather than assigning it via a pointer parameter?
>

A lot of the engineers at Lawrence Berkeley National Laboratory use
this method. I guess in my case, it's monkey see, monkey do. I don't
know if there is a formal computer science name to this technique or
if this is just some kind of strange Berkeley thing.

> The OP never bothered to tell us what "difference" means. *Actually, he
> didn't even use the word "difference"; he said "the value between two
> strings".
>
> > int main(void)
> > {
> > * char first[BUFSIZ];
> > * char second[BUFSIZ];

>
> Why would BUFSIZ (the size of the buffer used by the setbuf function)
> be a reasonable size for an array used for interactive input? *(It's
> 8192 on my system.)
>

I was drunk at the time at wrote the original code. It was really
hurting to think about anything at the time. So instead of trying to
think about a reasonable buffer size, I just used BUFSIZ.

> > * size_t f_len, s_len;
> > * char value;

>
> > * printf("Enter the first string: ");
> > * fflush(stdout);
> > * if (scanf("%s", first) == 1) {
> > * * f_len = strlen(first) + 1;
> > * } else {
> > * * fprintf(stderr, "Try again\n");
> > * }

>
> scanf() with a "%s" format is as dangerous as gets(). *It's also odd
> to ask for a string, but then read a whitespace-delimited word.
>

It was the first thing that popped into my head while I was under the
influence. In retrospect, would fgets() have been better?

> > * printf("Enter the second string: ");
> > * fflush(stdout);
> > * if (scanf("%s", second) == 1) {
> > * * s_len = strlen(second) + 1;
> > * } else {
> > * * fprintf(stderr, "Try again\n");
> > * }

>
> > * if (f_len == s_len) {

>
> The only thing you do with f_len and s_len is compare them here.
> You *could* just write
> * * if (strlen(first) == strlen(second) ...
>
> That's not to say that storing the lengths in variables is a bad thing
> -- but why did you add 1 to them?

I was starting to do case three[1], but then I quit because my mom was
calling me. When I got back to my computer, I totally lost my mojo to
write code.

[1]Case three is what Eric talked about. And I quote

"3) If the strings are of unequal length, you could pretend that the
shorter had a prefix of "aaa...aaa" to extend it to the length
of the longer. In this case it might be easier to work from
right to left and accumulate delta a little differently. "

Ben Bacarisse
Guest
Posts: n/a

 10-27-2010
Chad <(E-Mail Removed)> writes:

> On Oct 26, 7:08Â*pm, Keith Thompson <(E-Mail Removed)> wrote:
>> Chad <(E-Mail Removed)> writes:
>> >> Of course it does. Â*On some systems, your printf output will not be
>> >> visible until the fflush executes. Â*On those systems, your user will
>> >> not know why the system has stopped.

>>
>> > I don't know if the OP is still reading this, but how about something
>> > like the following (for case 1)

>>
>> > #include <stdio.h>
>> > #include <stdlib.h>
>> > #include <string.h>

>>
>> > void calculate(char *value, char first[], char second[])
>> > {
>> > Â* char difference = 0;
>> > Â* char delta = 0;

>>
>> > Â* for( ;*first && *second; first++,second++) {
>> > Â* Â* difference = *first - *second;
>> > Â* Â* delta Â*= delta * 26 + difference;
>> > Â* }
>> > Â* *value = delta;
>> > }

You are still returning a char. That unlikely to be able to represent
the result for everything but a few pairs of string.

>> Wouldn't it be more convenient to have the function return the result
>> rather than assigning it via a pointer parameter?

>
> A lot of the engineers at Lawrence Berkeley National Laboratory use
> this method. I guess in my case, it's monkey see, monkey do. I don't
> know if there is a formal computer science name to this technique or
> if this is just some kind of strange Berkeley thing.

It's worth thinking about Keith's question. Another way to think about
the same thing is this: what *would* make you write a function that
returns the result rather than using a pointer parameter? If the answer
is "nothing", why do programming languages have value-returning
functions at all? If you *can* think of reasons to return a value, why
do they not apply in this case?

If you can't say *why* the good folks at LBL write their programs the
way they do, you can't draw any lessons from their code. Learning by
copying code you don't understand is slow and error prone.

<snip>
--
Ben.

Keith Thompson
Guest
Posts: n/a

 10-27-2010
Chad <(E-Mail Removed)> writes:
> On Oct 26, 7:08Â*pm, Keith Thompson <(E-Mail Removed)> wrote:
>> Chad <(E-Mail Removed)> writes:

[...]
>> > void calculate(char *value, char first[], char second[])
>> > {
>> > Â* char difference = 0;
>> > Â* char delta = 0;

>>
>> > Â* for( ;*first && *second; first++,second++) {
>> > Â* Â* difference = *first - *second;
>> > Â* Â* delta Â*= delta * 26 + difference;
>> > Â* }
>> > Â* *value = delta;
>> > }

>>
>> Wouldn't it be more convenient to have the function return the result
>> rather than assigning it via a pointer parameter?

>
> A lot of the engineers at Lawrence Berkeley National Laboratory use
> this method. I guess in my case, it's monkey see, monkey do. I don't
> know if there is a formal computer science name to this technique or
> if this is just some kind of strange Berkeley thing.

A common style is to have functions return a result that indicates
whether they succeeded or not, which means that the actual result has to
be communicated via a pointer argument. For example, a square root
function in this style might look something like this:

int square_root(double *result, double x) {
if (x < 0.0) {
}
else {
*result = sqrt(x);
return OK;
}
}

My guess is that that's what's going on at LBNL, and you've
misunderstood it (it doesn't make much sense for a void function to be
written this way).

[...]

>> Why would BUFSIZ (the size of the buffer used by the setbuf function)
>> be a reasonable size for an array used for interactive input? Â*(It's
>> 8192 on my system.)

>
> I was drunk at the time at wrote the original code.

Ok, I give up.

If you're not able to write coherent code (or English), you always
have the option of *not writing anything*.

[...]

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"