Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

calculate value between two strings

 
 
John Green
Guest
Posts: n/a
 
      10-23-2010
Hi,

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

I tried:
printf("abc-aaa = %d\n","abc"-"aaa");
but I get -4.

How can I do what I want?

Thanks,
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-23-2010
On 10/24/10 09:51 AM, John Green wrote:
> Hi,
>
> I would like to calculate the value between two strings. For example:
> abc-aaa would be 29.
>
> I tried:
> printf("abc-aaa = %d\n","abc"-"aaa");
> but I get -4.


So the address of "abc" is 4 less than the address of "aaa", which isn't
surprising.

> How can I do what I want?


What do you want?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-23-2010
On 10/23/2010 4:51 PM, John Green wrote:
> Hi,
>
> I would like to calculate the value between two strings. For example:
> abc-aaa would be 29.


Did you mean 28? I'm going to assume that you want to treat
each string as a base-26 number with digits a=0,...,z=25; if that's
not your intent, please explain it more fully.

> I tried:
> printf("abc-aaa = %d\n","abc"-"aaa");
> but I get -4.


You might have gotten almost anything, including herpes.

> How can I do what I want?


A couple of ways, depending on what you already know about the
strings (e.g., will they always be of the same length, are they known
to be free of strange characters like '#', do you treat 'A' and 'a'
as equivalent, and so on). Here are a few sketches:

1) If the strings will always be of the same length, you could
march across them from left to right, comparing corresponding
characters. You'd find 'a'-'a' == 0, so you'd set delta=0.
At the next position you'd have 'b'-'a' == 1, and you'd set
delta = delta * 26 + 1 == 1. In the third position you'd get
'c'-'a' == 2, delta = delta * 26 + 2 == 28. Since the strings
are now exhausted, that's your final answer.

2) Method (1) has a flaw, because it assumes 'b'-'a' == 'c'-'b'
== ... == 'z'-'y' == 1. This is a very common scheme for
encoding letter values, but it is not universal. You could
salvage the method by looking up each character in a separate
string "abcdefghijklmnopqrstuvwxyz" and subtracting their
positions rather than their code values. The strchr() function
could be your friend here, and there are other possibilities too.

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.

4) A sneaky possibility: The strtol() function can be made to
treat the string as a base-36 number, with a=10,...,z=35. You
could convert each string to a number this way, subtract them,
and then fiddle with the answer to correct the 36-vs-26 bias.
This approach treats 'A' and 'a' as the same.

5) All the above methods assume that the strings are not "too long."
If they get long enough so that their values or differences
exceed the capacity of your machine's numbers, you'll need to get
more imaginative. For most machines strings of up to length 6
will be all right, while longer strings may encounter trouble.
With very little effort you could push the upper limit to 13, but
beyond that you'd have a good deal more work to do.

However, I have a sneaking suspicion that your current level of
expertise in C might not be adequate for any of these approaches. This
is not a slight on your character (there's no shame in starting to learn
something new to you), but I really, really think you should spend some
more time with your C textbook before you try to go much further.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-23-2010
John Green <(E-Mail Removed)> writes:
> I would like to calculate the value between two strings. For example:
> abc-aaa would be 29.
>
> I tried:
> printf("abc-aaa = %d\n","abc"-"aaa");
> but I get -4.
>
> How can I do what I want?


Start by clearly defining what you want.

C defines the difference between two numbers (it's simple
subtraction, though with some odd characteristics when you consider
overflow and/or unsigned types). It also defines the difference
between two pointer values (which is what your program happens
to be doing, but in a way that's several kinds of incorrect).
And you can subtract an integer from a pointer value, yielding a
new pointer value, but that's not what you want either.

You've shown us an example, "abc-aaa would be 29", which has led
some posters to speculate on what you might mean (which leads to a
result of 28, not 29). But that's only a guess. Another perfectly
plausible meaning, given the arguments "abc" and "aaa", would be
to treat both strings as hexadecimal representations, yielding 18
in this case.

Or, since you said "the value between two strings" rather than
"the difference between two strings", and string values can be
meaningfully ordered by strcmp(), the result could be anything from
"aab" to "abb", or even "aaazzzzzzzzzzzzzql" for that matter.

There is no obvious meaning for "the value between two strings",
and a single example isn't enough to specify one. It's up to you
to define (preferably not just by example) exactly what you mean.
Not only does it make it a lot easier for us to help you, it goes
a long way towards writing a solution in C. If you can write a
good English description of what you want to compute, you may be
able to translate that description into C.

Recommended reading:
http://www.catb.org/~esr/faqs/smart-questions.html

--
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"
 
Reply With Quote
 
Eyegor
Guest
Posts: n/a
 
      10-23-2010
On Oct 23, 6:30*pm, Keith Thompson <(E-Mail Removed)> wrote:
> John Green <(E-Mail Removed)> writes:
> > I would like to calculate the value between two strings. *For example:
> > abc-aaa would be 29.

>
> > I tried:
> > printf("abc-aaa = %d\n","abc"-"aaa");
> > but I get -4.

>
> > How can I do what I want?

>
> Start by clearly defining what you want.
>
> C defines the difference between two numbers (it's simple
> subtraction, though with some odd characteristics when you consider
> overflow and/or unsigned types). *It also defines the difference
> between two pointer values (which is what your program happens
> to be doing, but in a way that's several kinds of incorrect).
> And you can subtract an integer from a pointer value, yielding a
> new pointer value, but that's not what you want either.
>
> You've shown us an example, "abc-aaa would be 29", which has led
> some posters to speculate on what you might mean (which leads to a
> result of 28, not 29). *But that's only a guess. *Another perfectly
> plausible meaning, given the arguments "abc" and "aaa", would be
> to treat both strings as hexadecimal representations, yielding 18
> in this case.
>
> Or, since you said "the value between two strings" rather than
> "the difference between two strings", and string values can be
> meaningfully ordered by strcmp(), the result could be anything from
> "aab" to "abb", or even "aaazzzzzzzzzzzzzql" for that matter.
>
> There is no obvious meaning for "the value between two strings",
> and a single example isn't enough to specify one. *It's up to you
> to define (preferably not just by example) exactly what you mean.
> Not only does it make it a lot easier for us to help you, it goes
> a long way towards writing a solution in C. *If you can write a
> good English description of what you want to compute, you may be
> able to translate that description into C.
>
> Recommended reading:http://www.catb.org/~esr/faqs/smart-questions.html
>
> --
> 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"


Assuming your string variables are of the same length SL and your
strings are LSB aligned you can do the flowing:

X=0
for(i=SL; i--; i>0){
X=X+(S1[i]-S2[i])*26^(SL-i-1);
}
By default your strings are MSB aligned, so you have to create 2
strings, fill them with "nulls" and strcopy the input strings to the
most right section of addresses of the S1 and S2 buffers. SL is the
size in bytes of S1 and S2 Buffers.

 
Reply With Quote
 
Eyegor
Guest
Posts: n/a
 
      10-23-2010
On Oct 23, 7:31*pm, Eyegor <(E-Mail Removed)> wrote:
> On Oct 23, 6:30*pm, Keith Thompson <(E-Mail Removed)> wrote:
>
>
>
> > John Green <(E-Mail Removed)> writes:
> > > I would like to calculate the value between two strings. *For example:
> > > abc-aaa would be 29.

>
> > > I tried:
> > > printf("abc-aaa = %d\n","abc"-"aaa");
> > > but I get -4.

>
> > > How can I do what I want?

>
> > Start by clearly defining what you want.

>
> > C defines the difference between two numbers (it's simple
> > subtraction, though with some odd characteristics when you consider
> > overflow and/or unsigned types). *It also defines the difference
> > between two pointer values (which is what your program happens
> > to be doing, but in a way that's several kinds of incorrect).
> > And you can subtract an integer from a pointer value, yielding a
> > new pointer value, but that's not what you want either.

>
> > You've shown us an example, "abc-aaa would be 29", which has led
> > some posters to speculate on what you might mean (which leads to a
> > result of 28, not 29). *But that's only a guess. *Another perfectly
> > plausible meaning, given the arguments "abc" and "aaa", would be
> > to treat both strings as hexadecimal representations, yielding 18
> > in this case.

>
> > Or, since you said "the value between two strings" rather than
> > "the difference between two strings", and string values can be
> > meaningfully ordered by strcmp(), the result could be anything from
> > "aab" to "abb", or even "aaazzzzzzzzzzzzzql" for that matter.

>
> > There is no obvious meaning for "the value between two strings",
> > and a single example isn't enough to specify one. *It's up to you
> > to define (preferably not just by example) exactly what you mean.
> > Not only does it make it a lot easier for us to help you, it goes
> > a long way towards writing a solution in C. *If you can write a
> > good English description of what you want to compute, you may be
> > able to translate that description into C.

>
> > Recommended reading:http://www.catb.org/~esr/faqs/smart-questions.html

>
> > --
> > 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"

>
> Assuming your string variables are of the same length SL and your
> strings are LSB aligned you can do the flowing:
>
> X=0
> for(i=SL; i--; i>0){
> X=X+(S1[i]-S2[i])*26^(SL-i-1);}
>
> By default your strings are MSB aligned, so you have to create 2
> strings, fill them with "nulls" and strcopy the input strings to the
> most right section of addresses of the S1 and S2 buffers. SL is the
> size in bytes of S1 and S2 Buffers.


I know i used ^ as power, which is a no-no, but i got lazy.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-24-2010
Eyegor <(E-Mail Removed)> writes:
> On Oct 23, 6:30Â*pm, Keith Thompson <(E-Mail Removed)> wrote:
>> John Green <(E-Mail Removed)> writes:
>> > I would like to calculate the value between two strings. Â*For example:
>> > abc-aaa would be 29.

[...]
>> > How can I do what I want?

>>
>> Start by clearly defining what you want.

[...]
> Assuming your string variables are of the same length SL and your
> strings are LSB aligned you can do the flowing:
>
> X=0
> for(i=SL; i--; i>0){
> X=X+(S1[i]-S2[i])*26^(SL-i-1);
> }
> By default your strings are MSB aligned, so you have to create 2
> strings, fill them with "nulls" and strcopy the input strings to the
> most right section of addresses of the S1 and S2 buffers. SL is the
> size in bytes of S1 and S2 Buffers.


What??

You already acknowledged that "^" is xor, not exponentation. But what's
all this about the strings being "MSB aligned" or "LSB aligned"? As far
as I can tell, it just doesn't make any sense.

Given:
char s[] = "abc";
s[0] == 'a', s[1] == 'b', s[2] == 'c', and s[3] == '\0'. There's
no alignment to consider.

--
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"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-24-2010
Keith Thompson <(E-Mail Removed)> writes:

> Eyegor <(E-Mail Removed)> writes:
>> On Oct 23, 6:30Â*pm, Keith Thompson <(E-Mail Removed)> wrote:
>>> John Green <(E-Mail Removed)> writes:
>>> > I would like to calculate the value between two strings. Â*For example:
>>> > abc-aaa would be 29.

> [...]
>>> > How can I do what I want?
>>>
>>> Start by clearly defining what you want.

> [...]
>> Assuming your string variables are of the same length SL and your
>> strings are LSB aligned you can do the flowing:
>>
>> X=0
>> for(i=SL; i--; i>0){
>> X=X+(S1[i]-S2[i])*26^(SL-i-1);
>> }
>> By default your strings are MSB aligned, so you have to create 2
>> strings, fill them with "nulls" and strcopy the input strings to the
>> most right section of addresses of the S1 and S2 buffers. SL is the
>> size in bytes of S1 and S2 Buffers.

>
> What??
>
> You already acknowledged that "^" is xor, not exponentation. But what's
> all this about the strings being "MSB aligned" or "LSB aligned"? As far
> as I can tell, it just doesn't make any sense.


Following Seebs's advice, one might ask what could this notion of
alignment be since it clearly is not C's notion of it. I am pretty sure
that the OP means "lined up" rather than any C-like notion of alignment.

It's not easy to get to this conclusion because that condition is
redundant. Once the OP has said that the string must be the same
length, then their LSBs will "line up". Furthermore, when the OP says
"nulls" he means "whatever character is used to represent zero".
I.e. you must pad the shorter string with 'a's on the left.

The whole thing is a mess. Not only is there a ^ in place of pow(), but
the 'for' loop looks all wrong despite the fact that it works fine with
it's pointless third clause. The alignment condition uses the wrong
words and is redundant. There is no need to create 2 strings (you don't
need any in fact but, as described, the algorithm needs at more one of
them to be copied) and the padding with "nulls" is not right.

Oddly though, despite all that, I understood exactly what the OP meant
on first reading! I suppose that comes from years of looking at
students' programs.

<snip>
--
Ben.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-24-2010
On Sun, 24 Oct 2010 08:00:12 +0200, "io_x" <(E-Mail Removed)> wrote:

>
>"io_x" <(E-Mail Removed)> ha scritto nel messaggio
>news:4cc3bed2$0$26698$(E-Mail Removed) t...
>>
>> "John Green" <(E-Mail Removed)> ha scritto nel messaggio
>> news:(E-Mail Removed)...


snip

>
>/*-1 for error */
>unsigned value(char* a)
>{int i;
> unsigned tot;
>
> if(a==0) return -1;


value() returns an unsigned. -1 will be converted to UINT_MAX.

> for(i=0, tot=0; i>=0 && a[i]; ++i)


i starts at 0. i is incremented but never decremented. Does the test
i>=0 ever evaluate to 0?

> {tot+=a[i];
> if((int)tot<0) return -1;


tot is unsigned. It cannot be negative. (int)tot must be
non-negative if the value is between 0 and INT_MAX. If the value
exceeds INT_MAX, it can be negative only if that is the documented
implementation specific behavior. Do you know of any implementations
that do so? Will the test ever portably evaluate to 1?

> }
> if(i<0) return -1;


When will this test ever evaluate to 1?

> return tot;
>}
>


Or was it your intent to teach the OP as many bad ideas as possible?

--
Remove del for email
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      10-26-2010
On Oct 23, 2:19*pm, Eric Sosman <(E-Mail Removed)> wrote:
> On 10/23/2010 4:51 PM, John Green wrote:
>
> > Hi,

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

>
> * * *Did you mean 28? *I'm going to assume that you want to treat
> each string as a base-26 number with digits a=0,...,z=25; if that's
> not your intent, please explain it more fully.
>
> > I tried:
> > printf("abc-aaa = %d\n","abc"-"aaa");
> > but I get -4.

>
> * * *You might have gotten almost anything, including herpes.
>
> > How can I do what I want?

>
> * * *A couple of ways, depending on what you already know about the
> strings (e.g., will they always be of the same length, are they known
> to be free of strange characters like '#', do you treat 'A' and 'a'
> as equivalent, and so on). *Here are a few sketches:
>
> * * 1) If the strings will always be of the same length, you could
> * * * *march across them from left to right, comparing corresponding
> * * * *characters. *You'd find 'a'-'a' == 0, so you'd set delta=0.
> * * * *At the next position you'd have 'b'-'a' == 1, and you'd set
> * * * *delta = delta * 26 + 1 == 1. *In the third position you'd get
> * * * *'c'-'a' == 2, delta = delta * 26 + 2 == 28. *Since the strings
> * * * *are now exhausted, that's your final answer.
>
> * * *2) Method (1) has a flaw, because it assumes 'b'-'a' == 'c'-'b'
> * * * * == ... == 'z'-'y' == 1. *This is a very common scheme for
> * * * * encoding letter values, but it is not universal. *You could
> * * * * salvage the method by looking up each character in a separate
> * * * * string "abcdefghijklmnopqrstuvwxyz" and subtracting their
> * * * * positions rather than their code values. *The strchr() function
> * * * * could be your friend here, and there are other possibilities too.
>
> * * *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. 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;
char difference = 0;

for( ;*first && *second; *first++,*second++) {
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);
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;
}
[cdalten@localhost oakland]$ gcc -Wall -Wextra subtractstring.c -o
subtractstring
subtractstring.c: In function ‘calculate’:
subtractstring.c:10: warning: value computed is not used
subtractstring.c:10: warning: value computed is not used
[cdalten@localhost oakland]$ ./subtractstring
Enter the first string: aaa
Enter the second string: aaa
The difference between aaa - aaa is 0
[cdalten@localhost oakland]$ ./subtractstring
Enter the first string: aad
Enter the second string: aaa
The difference between aad - aaa is 3
[cdalten@localhost oakland]$ ./subtractstring
Enter the first string: abc
Enter the second string: aaa
The difference between abc - aaa is 28
[cdalten@localhost oakland]$

Yes, I could have automated the test. But like, ya know, this is
comp.lang.c. And like the standard doesn't cover such things.

Chad
 
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