A

#### arnuld

solution for it but i see errors and i tried to correct them but still

they are there and mostly are out of my head:

------------------------------- PROGRAMME --------------------------

/* Section 2.7 type conversions

we are asked to write a function "htoi(an array)" that accomplishes

this:

STATEMENT: this programme converts a string of hexadecimal digits inot

integers. a string may or may not contain "0X" or "0x" and

allowable digits are 0-9, a-f and A-F.

it runs in 4 steps:

1.) it take sinout from user using "for" loop in "main"

2.) "htoi" calls "heca without lead".

3.) "hexa_without_lead" removes leading "0x" or "0X", if any.

4.) it then calls "hexa_to_int" on "hexa_without_lead" to convert this

hexadeccimal number into an integer.

5.) "hexa_into_int" uses 2 helper functions: "arr_size" and

"base_conv"

to complete its job of convertint the hexadecimal number into an

integer.

*/

#include <stdio.h>

#include <ctype.h>

unsigned long int htoi(char input_array);

char hexa_without_lead(char arr[]);

unsigned long int hexa_to_int(char s[]);

int arr_size(char arr[]);

int main(void) {

int i, c;

unsigned long uli;

const int MAXELEMENTS = 1000;

char input_array[MAXELEMENTS];

for(i=0; i < MAXELEMENTS && ((c = getchar()) != EOF || c != '\n'); +

+i)

{

if( isdigit(c) || c == 'a' || c == 'A' || c == 'b' || c == 'B'

|| c == 'c' || c == 'C'

c == 'd' || c == 'D' || c == 'e' || c == 'E' || c == 'f' c == 'F' )

{

input_array

*= c;*

}

else

printf("\nYou are an IDIOT, i asked you to ENTER a hexadecimal number

\n");

}

if( c == '\n')

input_array

}

else

printf("\nYou are an IDIOT, i asked you to ENTER a hexadecimal number

\n");

}

if( c == '\n')

input_array

*= '\0';*

uli = htoi(input_array);

printf("\n\t the number entered is: %ld", uli);

return 0;

}

unsigned long int htoi(char input_array)

{

unsigned long uli;

char modified_hexa[];

modified_hexa[] = hexa_without_lead(input_array);

uli = hexa_to_int(modified_hexa);

return uli;

}

char hexa_without_lead(char arr[])

{

char s[];

int i;

for(i = 0; arruli = htoi(input_array);

printf("\n\t the number entered is: %ld", uli);

return 0;

}

unsigned long int htoi(char input_array)

{

unsigned long uli;

char modified_hexa[];

modified_hexa[] = hexa_without_lead(input_array);

uli = hexa_to_int(modified_hexa);

return uli;

}

char hexa_without_lead(char arr[])

{

char s[];

int i;

for(i = 0; arr

*!= '\0'; ++i)*

{

if(i == 0 && arr{

if(i == 0 && arr

*== '0' && ( arr[i+1] == 'x' || arr[i+1] ==*

'X'))

i = i+2;

s'X'))

i = i+2;

s

*= arr**;*

}

s}

s

*= '\0';*

return s;

}

unsigned long hexa_to_int(char s[])

{

unsigned long uli = 0;

int i, len;

enum { hexa_a = 10; hexa_b, hexa_c, hexa_d, hexa_e, hexa_f }; /*

this will give hexa_b = 11, hexa_c = 12...... */

len = array_size(s) - 1;

for(i=0; sreturn s;

}

unsigned long hexa_to_int(char s[])

{

unsigned long uli = 0;

int i, len;

enum { hexa_a = 10; hexa_b, hexa_c, hexa_d, hexa_e, hexa_f }; /*

this will give hexa_b = 11, hexa_c = 12...... */

len = array_size(s) - 1;

for(i=0; s

*!= '\0' && len >= 0; ++i)*

{

if(isdigit(s{

if(isdigit(s

*))*

uli = uli + (suli = uli + (s

** base_conv(len));*

else if(selse if(s

*== 'a' || 'A')*

uli = uli + (hexa_a * base_conv(len));

else if(suli = uli + (hexa_a * base_conv(len));

else if(s

*== 'b' || 'B')*

uli = uli + ( hexa_b * base_conv(len) );

else if(suli = uli + ( hexa_b * base_conv(len) );

else if(s

*== 'c' || 'C')*

uli = uli + ( hexa_c * base_conv(len) );

else if(suli = uli + ( hexa_c * base_conv(len) );

else if(s

*== 'd' || 'D')*

uli = uli + ( hexa_d * base_conv(len) );

else if(suli = uli + ( hexa_d * base_conv(len) );

else if(s

*== 'e' || 'E')*

uli = uli + ( hexa_e * base_conv(len) );

else

uli = uli + ( hexa_f * base_conv(len) );

--len;

}

return uli;

}

unsigned long base_conv(int n)

{

int base_unit = 16;

while(n >= 0)

{

if(n == 0)

base_unit = base_unit * 1;

else

base_unit = base_unit * base_unit;

--n;

}

return base_unit;

}

int arr_size(char arr[])

{

int i;

for(i=0; arruli = uli + ( hexa_e * base_conv(len) );

else

uli = uli + ( hexa_f * base_conv(len) );

--len;

}

return uli;

}

unsigned long base_conv(int n)

{

int base_unit = 16;

while(n >= 0)

{

if(n == 0)

base_unit = base_unit * 1;

else

base_unit = base_unit * base_unit;

--n;

}

return base_unit;

}

int arr_size(char arr[])

{

int i;

for(i=0; arr

*!= EOF; ++i)*

;

return i;

}

-------------------------------- OUTPUT

---------------------------------------

[arch@voodo kr2]$ gcc -std=c99 -Wall -Wextra 27_ex-2-3.c

27_ex-2-3.c: In function 'main':

27_ex-2-3.c:44: error: expected ')' before 'c'

27_ex-2-3.c:57: warning: passing argument 1 of 'htoi' makes integer

from pointer without a cast

27_ex-2-3.c: In function 'htoi':

27_ex-2-3.c:69: error: array size missing in 'modified_hexa'

27_ex-2-3.c:72: error: expected expression before ']' token

27_ex-2-3.c:72: warning: passing argument 1 of 'hexa_without_lead'

makes pointer from integer without a cast

27_ex-2-3.c: In function 'hexa_without_lead':

27_ex-2-3.c:82: error: array size missing in 's'

27_ex-2-3.c:94: warning: return makes integer from pointer without a

cast

27_ex-2-3.c:94: warning: function returns address of local variable

27_ex-2-3.c: In function 'hexa_to_int':

27_ex-2-3.c:103: error: expected ',' or '}' before ';' token

27_ex-2-3.c:106: warning: implicit declaration of function

'array_size'

27_ex-2-3.c:111: warning: implicit declaration of function 'base_conv'

27_ex-2-3.c:117: error: 'hexa_b' undeclared (first use in this

function)

27_ex-2-3.c:117: error: (Each undeclared identifier is reported only

once

27_ex-2-3.c:117: error: for each function it appears in.)

27_ex-2-3.c:120: error: 'hexa_c' undeclared (first use in this

function)

27_ex-2-3.c:123: error: 'hexa_d' undeclared (first use in this

function)

27_ex-2-3.c:126: error: 'hexa_e' undeclared (first use in this

function)

27_ex-2-3.c:129: error: 'hexa_f' undeclared (first use in this

function)

27_ex-2-3.c: At top level:

27_ex-2-3.c:139: error: conflicting types for 'base_conv'

27_ex-2-3.c:111: error: previous implicit declaration of 'base_conv'

was here

[arch@voodo kr2]$;

return i;

}

-------------------------------- OUTPUT

---------------------------------------

[arch@voodo kr2]$ gcc -std=c99 -Wall -Wextra 27_ex-2-3.c

27_ex-2-3.c: In function 'main':

27_ex-2-3.c:44: error: expected ')' before 'c'

27_ex-2-3.c:57: warning: passing argument 1 of 'htoi' makes integer

from pointer without a cast

27_ex-2-3.c: In function 'htoi':

27_ex-2-3.c:69: error: array size missing in 'modified_hexa'

27_ex-2-3.c:72: error: expected expression before ']' token

27_ex-2-3.c:72: warning: passing argument 1 of 'hexa_without_lead'

makes pointer from integer without a cast

27_ex-2-3.c: In function 'hexa_without_lead':

27_ex-2-3.c:82: error: array size missing in 's'

27_ex-2-3.c:94: warning: return makes integer from pointer without a

cast

27_ex-2-3.c:94: warning: function returns address of local variable

27_ex-2-3.c: In function 'hexa_to_int':

27_ex-2-3.c:103: error: expected ',' or '}' before ';' token

27_ex-2-3.c:106: warning: implicit declaration of function

'array_size'

27_ex-2-3.c:111: warning: implicit declaration of function 'base_conv'

27_ex-2-3.c:117: error: 'hexa_b' undeclared (first use in this

function)

27_ex-2-3.c:117: error: (Each undeclared identifier is reported only

once

27_ex-2-3.c:117: error: for each function it appears in.)

27_ex-2-3.c:120: error: 'hexa_c' undeclared (first use in this

function)

27_ex-2-3.c:123: error: 'hexa_d' undeclared (first use in this

function)

27_ex-2-3.c:126: error: 'hexa_e' undeclared (first use in this

function)

27_ex-2-3.c:129: error: 'hexa_f' undeclared (first use in this

function)

27_ex-2-3.c: At top level:

27_ex-2-3.c:139: error: conflicting types for 'base_conv'

27_ex-2-3.c:111: error: previous implicit declaration of 'base_conv'

was here

[arch@voodo kr2]$