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-30-2010
On Oct 29, 9:42*am, "FredK" <(E-Mail Removed)> wrote:
> "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).


And in retrospect, I guess I would use fgets() instead of scanf().
Maybe something like the following...

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

#define MAX 10
int main(void)
{
char *buf;
char first[MAX];
char second[MAX];

printf("Input the first string:");
fflush(stdout);
if (fgets(first, MAX, stdin) != NULL) {
if (strlen(first) + 1 >= MAX) {
fprintf(stderr, "The first string is too long\n");
exit(EXIT_FAILURE);
} else if ((buf = strchr(first, '\n')) != NULL) {
*buf = '\0';
printf ("The first string is: %s\n", first);
}
}

printf("Input the second string:");
fflush(stdout);
if (fgets(second, MAX, stdin) != NULL) {
if (strlen(second) + 1 >= MAX) {
fprintf(stderr, "The second string is too long\n");
exit(EXIT_FAILURE);
} else if ((buf = strchr(second, '\n')) != NULL) {
*buf = '\0';
printf ("The second string is: %s\n", second);
}
}

exit(EXIT_SUCCESS);
}
[cdalten@localhost oakland]$ gcc -Wall -Wextra getin.c -o getin
[cdalten@localhost oakland]$ ./getin
Input the first string:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The first string is too long
[cdalten@localhost oakland]$ ./getin
Input the first string:a
The first string is: a
Input the second string:b
The second string is: b
[cdalten@localhost oakland]$ ./getin
Input the first string:a
The first string is: a
Input the second
string:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
The second string is too long
[cdalten@localhost oakland]$



 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      10-30-2010
On 26 Oct, 02:55, 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>

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

>
> > This is one statement too late.

>
> Does the order matter in this case?


yes. Assuming you expect the fflush() to be forcing the prompt to
appear before the scanf() tries to do the read. If you think the
fflush() is doing something to the input then you are in a state of
sin...

Oh, and scanf() with a %s format specifier is morally equivalent to
gets().

There is a special circle in hell reserved for such programmers.
Probably involves old COBOL.
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      10-30-2010
On 27 Oct, 04:04, Ben Bacarisse <(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:
> >> >> 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.


one possible reason is the return value is used as an error code (of
course Chad didn't do this)
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-02-2010
On Mon, 1 Nov 2010 17:02:45 +0100, "io_x" <(E-Mail Removed)> wrote:

>
>"io_x" <(E-Mail Removed)> ha scritto nel messaggio
>news:4cc3cb99$0$26699$(E-Mail Removed) t...
>> "io_x" <(E-Mail Removed)> ha scritto nel messaggio
>> news:4cc3bed2$0$26698$(E-Mail Removed) ...
>>> "John Green" <(E-Mail Removed)> ha scritto nel messaggio
>>> news:(E-Mail Removed)...
>>>> 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");

>>
>> in the fly
>>
>> /*-1 for error */
>> unsigned value(char* a)
>> {int i;
>> unsigned tot;
>>
>> if(a==0) return -1;
>> for(i=0, tot=0; i>=0 && a[i]; ++i)
>> {tot+=a[i];
>> if((int)tot<0) return -1;

>
>Are there systems where tot+=a[i] overflow tot and tot>0?
>i think not


Think instead. If tot+=a[i] happens to exceed UINT_MAX, the resulting
value in tot will still be non-negative. So the answer to your
question is: Yes, every C system in the world as long as the sum is
not exactly equal to UINT_MAX+1.

>the same for i
>the wrost result is some seg fault for the overflow


Hardly. By far a worse result is to produce a garbage value and have
your program blissfully proceed without realizing there is a problem.


--
Remove del for email
 
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