Nezhate said:
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.
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;
}
}
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);
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)));
But if you're planning to use "r" for other things as you modify the
code, it's good to have it.
"return 0;" would also work here.