Re: atoi



Nezhate <mazouz.nezhate@xxxxxxxxx> writes:
Thanks to all for their help.
I found the solution. In order to not use atoi function with an array
of characters, I wrote a small function, which uses the Ascii code of
character under test. Here it is:

I have a bit of constructive criticism.

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

int char2int(char c)
{
int num;
if ((c>='0')&&(c<='9'))

The extra parentheses really aren't necessary. The >= and <=
operators bind more tightly than &&. I'd write this with some extra
whitespace as:

if (c >= '0' && c <= '9')

or maybe:

if (c>='0' && c<='9')

Or you could add "#include <ctype.h>" and use isdigit() here.

As it happens, the method you use here is guaranteed to work, but the
corresponding technique ((c>='a')&&(c<='z')) is *not* guaranteed to
work for letters. It's a good idea to get into the habit of using the
<ctype.h> functions for character classification.

Note that isdigit() expects an int argument that's representable as an
unsigned char, so you'd need to write:

if (isdigit((unsigned char)c))

num=c-'0';
else
{
printf("Character \"%c\" is not a number\n",c);

It doesn't matter so much for a small program like this, but in
general utility functions shouldn't print error messages. (Also,
error messages should usually go to stderr, not stdout.) Here, you
know exactly how the function will be used, but you don't always have
that luxury, so you want to give the caller a bit more flexibility.
Use some method to tell *the caller*, not the user, that there was an
error, and let the caller handle it.


num=0;
}
return (num);

The parentheses aren't necessary.

}

Here's how I'd write the char2int function:

/*
* Returns value of a digit ('5' -> 5, for example).
* Returns -1 for a non-digit.
*/
int char2int(char c)
{
if (isdigit((unsigned char)c)) {
return c - '0';
}
else {
return -1;
}
}

int main ()

Make this "int main(void)". "int main()" is almost certainly ok, but
it's better to be explicit.

{
int i;
char line [100];
printf ("Enter a line to test\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0';

Here you replace the '\n' left in the string by fgets() with '\0'.
But fgets() doesn't *always* leave a '\n' in the string. (Exercise:
figure out under what circumstances it doesn't.)

for (i=0;i<strlen(line);i++)

Here you have a fairly nasty inefficiency, though it's not something
that's going to make any visible difference in this program. You call
strlen(line) every time through the loop. The strlen() function has
to scan the string from the beginning to find the terminating '\0'
character every time it's called. You've turned an O(N) algorithm
into an O(N**2) algorithm. Since the value of strlen(line) doesn't
change during the loop, this is wasteful.

Save the value of strlen(line) in a variable and test it.

Note that you do change the length of the string when you replace the
'\n' with '\0'. If you want to be just a little bit tricky, you could
do something like this:

size_t len;
...
fgets(line,sizeof(line),stdin);
len = strlen(line);

line[strlen(line)-1] = '\0';
len --;

{
int r=char2int(line[i]);
printf("line[%d]=%d\n",i,r);

Note that you don't really need "r"; you could write:

printf("line[%d]=%d\n", i, char2int(line[i])));

But if you're planning to use "r" for other things as you modify the
code, it's good to have it.


}
exit(0);

"return 0;" would also work here.

}

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Standard function to convert " " to (etc.)?
    ... decodes all these escapes back into a string. ... corresponding character. ... int convert_escape{ ... #define ESCAPE 1 ...
    (comp.lang.c)
  • RE: DTS How to parse a varcharfield
    ... Nick Barclay created a UDF function that parses a varchar field. ... It returns the position of the character AFTER the nth(i.e. ... you know the positions you could update the varchar string by substituting ... returns int ...
    (microsoft.public.sqlserver.dts)
  • Re: RC4 algorithm problem
    ... > int lengthOfData; ... default character encoding, at least when any of the characters are out ... Is using an ASCII string directly as the RC4 key a recommended practice? ...
    (sci.crypt)
  • Re: Strange strcmp() problem
    ... I would *guess* that you failed to notice an opening '(' character on your test string. ... How will your program react to a number greater than int being put in? ... Many experts consider that the best way to capture input is to accept the input characters into a string, and then pick the string apart in more detail - that is, to separate the task of capture from the task of validation. ... All of these are palindromes, but none will pass your test. ...
    (comp.lang.c)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... add some more string functions on the model of the function ... void UppStrg(char *Low, char *Upp, int cnt); ... Once you've detected a character is not lower case, you want to break out of the inner loop. ...
    (comp.lang.c)