Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > calculate value between two strings

Reply
Thread Tools

calculate value between two strings

 
 
Chad
Guest
Posts: n/a
 
      10-27-2010
On Oct 26, 10:52*pm, Keith Thompson <(E-Mail Removed)> wrote:
> 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) {
> * * * * * * return BAD_ARGUMENT;
> * * * * }
> * * * * 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).
>
> [...]
>


Later on today, when I get home from work, I'll try and find some of
the C code from LBNL that uses this type of programming style.


 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      10-28-2010
Let's being the next round of below average C code. I guess I just
wanted to prove to myself that I can write C code without having to
resort to google or any kind of computer programming book for help.
With that, here is my attempt to attack the case when the input
strings aren't of equal length...

[cdalten@localhost oakland]$ more sub2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void calculate_el(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;
}

void calculate_ul(char *value, char first[], char second[], char
max[], \
size_t f_len, size_t s_len)
{
size_t l_diff;

if ( f_len > s_len) {
l_diff = f_len - s_len;
strncat(second, max ,l_diff);
} else {
l_diff = s_len - f_len;
strncat(first, max, l_diff);
}

calculate_el(value, first, second);
}

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

char
max[]="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaa";
max_value = strlen(max);

printf("Enter the first string: ");
fflush(stdout);
if (scanf("%s", first) == 1) {
f_len = strlen(first);
if (f_len > max_value) {
fprintf(stderr, "The first input string is too long\n");
exit(1);
}
} else {
fprintf(stderr, "Try again\n");
}

printf("Enter the second string: ");
fflush(stdout);
if (scanf("%s", second) == 1) {
s_len = strlen(second);
if (s_len > max_value) {
fprintf(stderr, "The second input string is too long\n");
exit(1);
}
} else {
fprintf(stderr, "Try again\n");
}

if (f_len == s_len) {
calculate_el(&value, first, second);
printf("The difference between %s - %s is %d\n", first, second,
value);
} else {
calculate_ul(&value, first, second, max, f_len, s_len);
printf("The difference between %s - %s is %d\n", first, second,
value);
}

exit(1);
}
[cdalten@localhost oakland]$ gcc -Wall -Wextra sub2.c -o sub2
[cdalten@localhost oakland]$ ./sub2
Enter the first string: aaa
Enter the second string: aaa
The difference between aaa - aaa is 0
[cdalten@localhost oakland]$ ./sub2
Enter the first string: aad
Enter the second string: aaa
The difference between aad - aaa is 3
[cdalten@localhost oakland]$ ./sub2
Enter the first string: abc
Enter the second string: aaa
The difference between abc - aaa is 28
[cdalten@localhost oakland]$ ./sub2
Enter the first string: a
Enter the second string: abc
The difference between aaa - abc is -28
[cdalten@localhost oakland]$ ./sub2
Enter the first string: abc
Enter the second string: a
The difference between abc - aaa is 28
[cdalten@localhost oakland]$ ./sub2
Enter the first string:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaa
The first input string is too long
[cdalten@localhost oakland]$ ./sub2
Enter the first string: b
Enter the second string:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The second input string is too long
[cdalten@localhost oakland]$
 
Reply With Quote
 
 
 
 
Chad
Guest
Posts: n/a
 
      10-28-2010
On Oct 27, 6:45*pm, Chad <(E-Mail Removed)> wrote:
> Let's being the next round of below average C code. I guess I just
> wanted to prove to myself that I can write C code without having to
> resort to google or any kind of computer programming book for help.
> With that, here is my attempt to attack the case when the input
> strings aren't of equal length...


I think the next time I'm drunk, I'm going to configure my editor so
like, when I copy and paste from my editor to comp.lang.c via the
google web interface, the code formatting will stay intact.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2010
Chad <(E-Mail Removed)> writes:
[...]
> I think the next time I'm drunk, I'm going to configure my editor so
> like, when I copy and paste from my editor to comp.lang.c via the
> google web interface, the code formatting will stay intact.


Next time you're drunk, *unplug your computer*.

--
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"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-28-2010
On Wed, 27 Oct 2010 18:45:41 -0700 (PDT), Chad <(E-Mail Removed)>
wrote:

>Let's being the next round of below average C code. I guess I just
>wanted to prove to myself that I can write C code without having to
>resort to google or any kind of computer programming book for help.
>With that, here is my attempt to attack the case when the input
>strings aren't of equal length...
>
>[cdalten@localhost oakland]$ more sub2.c
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>void calculate_el(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;
>}
>
>void calculate_ul(char *value, char first[], char second[], char
>max[], \


What purpose do you think the \ serves? What would be different if
you left it out?

> size_t f_len, size_t s_len)
>{
> size_t l_diff;
>
> if ( f_len > s_len) {
> l_diff = f_len - s_len;
> strncat(second, max ,l_diff);


What makes you think that in general it is safe to append data to the
caller's array?

> } else {
> l_diff = s_len - f_len;
> strncat(first, max, l_diff);
> }
>
> calculate_el(value, first, second);
>}
>
>int main(void)
>{


snip

> exit(1);
>}


Why are you returning a non-portable value? Why is one normally not
associated with success?

snip

>[cdalten@localhost oakland]$ ./sub2
>Enter the first string: b
>Enter the second string:
>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
>The second input string is too long
>[cdalten@localhost oakland]$


It is a shame you didn't try to b and aaa to see the folly of trying
to store the result in a char.

--
Remove del for email
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      10-28-2010
On Oct 27, 10:11*pm, Barry Schwarz <(E-Mail Removed)> wrote:
> On Wed, 27 Oct 2010 18:45:41 -0700 (PDT), Chad <(E-Mail Removed)>
> wrote:
>
>
>
>
>
> >Let's being the next round of below average C code. I guess I just
> >wanted to prove to myself that I can write C code without having to
> >resort to google or any kind of computer programming book for help.
> >With that, here is my attempt to attack the case when the input
> >strings aren't of equal length...

>
> >[cdalten@localhost oakland]$ more sub2.c
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include <string.h>

>
> >void calculate_el(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;
> >}

>
> >void calculate_ul(char *value, char first[], char second[], char
> >max[], \

>
> What purpose do you think the \ serves? *What would be different if
> you left it out?
>


I used the '\' character because the line wrap feature on my editor
doesn't play well with other editors line wrap feature.

> > * * * * * * * * *size_t f_len, size_t s_len)
> >{
> > * size_t l_diff;

>
> > *if ( f_len > s_len) {
> > * *l_diff = f_len - s_len;
> > * *strncat(second, max ,l_diff);

>
> What makes you think that in general it is safe to append data to the
> caller's array?
>


I don't . What's a better method?

> > *} else {
> > * *l_diff = s_len - f_len;
> > * *strncat(first, max, l_diff);
> > *}

>
> > *calculate_el(value, first, second);
> >}

>
> >int main(void)
> >{

>
> snip
>
> > *exit(1);
> >}

>
> Why are you returning a non-portable value? *Why is one normally not
> associated with success?
>


Because I was too lazy to type 'EXIT_FAILURE'.


 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      10-28-2010
On 2010-10-28, Chad <(E-Mail Removed)> wrote:
>> >void calculate_ul(char *value, char first[], char second[], char
>> >max[], \


> I used the '\' character because the line wrap feature on my editor
> doesn't play well with other editors line wrap feature.


You missed the point of the question.

You don't need a \ there at all with or without any line wrapping.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-29-2010
On Thu, 28 Oct 2010 12:25:24 -0700 (PDT), Chad <(E-Mail Removed)>
wrote:

>On Oct 27, 10:11*pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On Wed, 27 Oct 2010 18:45:41 -0700 (PDT), Chad <(E-Mail Removed)>
>> wrote:


snip

>> >void calculate_ul(char *value, char first[], char second[], char
>> >max[], \

>>
>> What purpose do you think the \ serves? *What would be different if
>> you left it out?
>>

>
>I used the '\' character because the line wrap feature on my editor
>doesn't play well with other editors line wrap feature.
>
>> > * * * * * * * * *size_t f_len, size_t s_len)


The point I was trying to make was that you don't need to concatenate
lines when the break occurs between tokens. You could just as easily
have typed
void calculate_ul(char *value,
char first[],
...
size_t s_len)
with no \ in sight and achieved the same result. The compiler will
strip out irrelevant white space.

>> >{
>> > * size_t l_diff;

>>
>> > *if ( f_len > s_len) {
>> > * *l_diff = f_len - s_len;
>> > * *strncat(second, max ,l_diff);

>>
>> What makes you think that in general it is safe to append data to the
>> caller's array?
>>

>
>I don't . What's a better method?


Either allocate or define an array of sufficient size and
copy/concatenate into that.

>
>> > *} else {
>> > * *l_diff = s_len - f_len;
>> > * *strncat(first, max, l_diff);
>> > *}

>>
>> > *calculate_el(value, first, second);
>> >}

>>
>> >int main(void)
>> >{

>>
>> snip
>>
>> > *exit(1);
>> >}

>>
>> Why are you returning a non-portable value? *Why is one normally not
>> associated with success?
>>

>
>Because I was too lazy to type 'EXIT_FAILURE'.


Why would you use EXIT_FAILURE when the program worked successfully?
If you don't want to type EXIT_SUCCESS, 0 is guaranteed to have the
same effect.

--
Remove del for email
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-29-2010
Chad <(E-Mail Removed)> writes:

> On Oct 27, 10:11*pm, Barry Schwarz <(E-Mail Removed)> wrote:
>> On Wed, 27 Oct 2010 18:45:41 -0700 (PDT), Chad <(E-Mail Removed)>
>> wrote:

<snip>
>> >void calculate_el(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;
>> >}

<snip>
>> > * size_t l_diff;

>>
>> > *if ( f_len > s_len) {
>> > * *l_diff = f_len - s_len;
>> > * *strncat(second, max ,l_diff);

>>
>> What makes you think that in general it is safe to append data to the
>> caller's array?

>
> I don't . What's a better method?


It's better to avoid having to alter or copy either string. I'd do
this:

long long str_diff(const char *s1, const char *s2)
{
const char *e1 = strchr(s1, 0);
const char *e2 = strchr(s2, 0);
long long diff = 0;
for (long long p = 1; e1 > s1 || e2 > s2; p *= 26)
diff += ((e1 == s1 ? 'a' : *--e1) - (e2 == s2 ? 'a' : *--e2)) * p;
return diff;
}

I.e. working backwards and faking the presumed leading 'a's.

<snip>
--
Ben.
 
Reply With Quote
 
FredK
Guest
Posts: n/a
 
      10-29-2010

"Chad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Oct 26, 7:08 pm, Keith Thompson <(E-Mail Removed)> wrote:
> Chad <(E-Mail Removed)> writes:


[snip]

>
> 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 is not an uncommon convention in some programming environments that the
return from all procedure calls is a status code (or none if there is no
meaningful status).



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calculate difference between two timestamps [newbie] nukeymusic Python 10 12-18-2011 03:22 PM
calculate the number of days between two dates Luca Villa Perl Misc 2 11-11-2007 11:51 AM
Calculate number of non-weekend days between two dates? chrismo Ruby 2 01-16-2007 09:04 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
calculate day's between two dates Tom C++ 6 03-16-2006 06:07 PM



Advertisments