#include <stdio.h> // for printf(), scanf()
#define LENGTH 15 // max length of string
int atoi(char s[]); // string to integer
int main()
{
char s[LENGTH];
printf("Enter a decimal number [+-]?[0-9]+\n");
scanf("%s", s);
printf("atoi(%s): %d\n", s, atoi(s));
return 0;
}
int atoi(char s[]) // string to integer (decimal)
{
if (s[0] == '\0') // empty string
{ // not a valid decimal
printf("\"%s\" is not a valid number\n", s);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here s[] is not empty
int i = 0, n = 0, sign = 1;
if (s[i] == '-') // negative number
{
sign = -1;
i++; // skip optional sign
}
else if (s[i] == '+') // positive number
{
i++; // skip optional sign
}
if (s[i] == '\0')
{
printf("\"%s\" is not a valid decimal number\n", s);
return 0;
}
while (s[i] != '\0')
{
if (s[i] >= '0' && s[i] <= '9')
{n = n*10 + (s[i]-'0');}
else // not a valid decimal
{
printf("\"%s\" is not a valid decimal number\n", s);
return sign * n;
}
i++;
}
return sign * n;
}
/*
gcc atoi.c -o atoi
./atoi
Enter a decimal number [+-]?[0-9]+
0
atoi(0): 0
./atoi
Enter a decimal number [+-]?[0-9]+
-1
atoi(-1): -1
./atoi
Enter a decimal number [+-]?[0-9]+
+2
atoi(+2): 2
./atoi
Enter a decimal number [+-]?[0-9]+
a
"a" is not a valid decimal number
atoi(a): 0
./atoi
Enter a decimal number [+-]?[0-9]+
+
"+" is not a valid decimal number
atoi(+): 0
./atoi
Enter a decimal number [+-]?[0-9]+
-
"-" is not a valid decimal number
atoi(-): 0
./atoi
Enter a decimal number [+-]?[0-9]+
1.2
"1.2" is not a valid decimal number
atoi(1.2): 1
./atoi
Enter a decimal number [+-]?[0-9]+
-1.2
"-1.2" is not a valid decimal number
atoi(-1.2): -1
./atoi
Enter a decimal number [+-]?[0-9]+
32767 // SHRT_MAX (limits.h)
atoi(32767): 32767
./atoi
Enter a decimal number [+-]?[0-9]+
2147483647 // INT_MAX
atoi(2147483647): 2147483647
./atoi
Enter a decimal number [+-]?[0-9]+
2147483648 // INT_MAX + 1
atoi(2147483648): -2147483648 // INT_MIN (modulo 2 arithmetic)
./atoi
Enter a decimal number [+-]?[0-9]+
-2147483648 // INT_MIN
atoi(-2147483648): -2147483648
./atoi
Enter a decimal number [+-]?[0-9]+
-2147483649 // INT_MIN - 1
atoi(-2147483649): 2147483647 // INT_MAX (modulo 2 arithmetic)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 20 // max length of string
unsigned atou(char s[]); // string to unsigned integer (decimal)
int main()
{
char s[LENGTH];
printf("Enter an unsigned decimal number [+]?[0-9]+\n");
scanf("%s", s);
printf("atou(%s): %u\n", s, atou(s));
return 0;
}
unsigned atou(char s[]) // string to unsigned integer (decimal)
{
if (s[0] == '\0') // empty string
{ // not a valid decimal
printf("\"%s\" is not a valid number\n", s);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here s[] is not empty
int i = 0, n = 0;
if (s[i] == '+')
{
i++; // skip optional sign
}
if (s[i] == '\0' || s[i] == '-' && s[i+1] == '\0')
{
printf("\"%s\" is not a valid number\n", s);
return 0;
}
if (s[i] == '-' && s[i+1] == '0' && s[i+2] == '\0')
{return 0;}
while (s[i] != '\0')
{
if (s[i] >= '0' && s[i] <= '9')
{n = n*10 + (s[i] - '0');}
else // not a valid decimal
{
printf("\"%s\" is not a valid unsigned decimal number\n", s);
return n;
}
i++;
}
return n;
}
/*
gcc atou.c -o atou
./atou
Enter an unsigned decimal number [+]?[0-9]+
0
atou(0): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
-0
atou(-0): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
-0.0
"-0.0" is not a valid unsigned decimal number
atou(-0.0): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
-1
"-1" is not a valid unsigned decimal number
atou(-1): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
+0
atou(+0): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
+2
atou(+2): 2
./atou
Enter an unsigned decimal number [+]?[0-9]+
a
"a" is not a valid unsigned decimal number
atou(a): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
+
"+" is not a valid number
atou(+): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
-
"-" is not a valid number
atou(-): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
1.2
"1.2" is not a valid unsigned decimal number
atou(1.2): 1
./atou
Enter an unsigned decimal number [+]?[0-9]+
-1.2
"-1.2" is not a valid unsigned decimal number
atou(-1.2): 0
./atou
Enter an unsigned decimal number [+]?[0-9]+
65535 // USHRT_MAX (limits.h)
atou(65535): 65535
./atou
Enter an unsigned decimal number [+]?[0-9]+
4294967295 // UINT_MAX
atou(4294967295): 4294967295
./atou
Enter an unsigned decimal number [+]?[0-9]+
4294967296 // UINT_MAX+1
atou(4294967296): 0 // modulo 2 arithmetic
./atou
Enter an unsigned decimal number [+]?[0-9]+
4294967297 // UINT_MAX+2
atou(4294967297): 1 // modulo 2 arithmetic
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
void lower(char []); // to lower case
void upper(char []); // to upper case
int main()
{
char s[LENGTH];
printf("Enter a word in lower case: ");
scanf("%s", s);
printf("upper(%s) = ", s);
upper(s);
printf("%s\n", s);
printf("Enter a word in upper case: ");
scanf("%s", s);
printf("lower(%s) = ", s);
lower(s);
printf("%s\n", s);
return 0;
}
void lower(char s[]) // to lower case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
{s[i] = s[i] + 'a' - 'A';} // 'a' > 'A' in ASCII
}
}
void upper(char s[]) // to upper case
{
int i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= 'a' && s[i] <= 'z')
{s[i] = s[i] - 'a' + 'A';} // s[i] - ('a' - 'A')
}
}
/*
gcc case.c -o case
./case
Enter a word in lower case: Hello,
upper(Hello,) = HELLO,
Enter a word in upper case: World!
lower(World!) = world!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-3. Write the function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F.
#include <stdio.h> // for printf(), scanf()
#define LENGTH 15 // max length of hex string (2 hex digits per byte)
int htoi(char hex[]); // hex (string) to decimal (integer)
int main()
{
char s[LENGTH];
printf("Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+\n");
scanf("%s", s);
printf("htoi(%s): %d\n", s, htoi(s));
return 0;
}
int htoi(char hex[]) // hex (string) to decimal (integer)
{
if (hex[0] == '\0') // empty string
{ // not a number
printf("\"%s\" is not a valid number\n", hex);
return 0; // return value does not signal error, should use an exception
}
// here hex[] is not empty
int i = 0, n = 0, sign = 1;
char temp;
if (hex[i] == '-') // negative number
{
sign = -1;
i++; // skip optional sign
}
else if (hex[i] == '+') // positive number
{
i++; // skip optional sign
}
if (hex[i] == '0')
{
i++;
if (hex[i] == '\0')
{
return n; // return 0; // valid hex number
}
if (hex[i] == 'x' || hex[i] == 'X')
{i++;} // skip optional 0x or 0X
}
else if (hex[i] == 'o' || hex[i] == 'O')
{
temp = hex[i];
i++;
if (hex[i] == 'x' || hex[i] == 'X')
{
printf("\"%s\" is not a valid hex number\n", hex);
hex[i-1] = '0';
printf("Did you mean \"%s\"?\n", hex);
hex[i-1] = temp;
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
}
if (hex[i] == '\0')
{
printf("\"%s\" is not a valid hex number\n", hex);
return 0;
}
while (hex[i] != '\0')
{
if (hex[i] >= '0' && hex[i] <= '9')
{n = n*16 + (hex[i]-'0');}
else if (hex[i] >= 'a' && hex[i] <= 'f')
{n = n*16 + (hex[i]-'a'+10);}
else if (hex[i] >= 'A' && hex[i] <= 'F')
{n = n*16 + (hex[i]-'A'+10);}
else // not a hex number
{
printf("\"%s\" is not a valid hex number\n", hex);
return sign * n;
}
i++;
}
return sign * n;
}
/*
gcc htoi.c -o htoi
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0
htoi(0): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x
"0x" is not a valid hex number
htoi(0x): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
1.2
"1.2" is not a valid hex number
htoi(1.2): 1
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-2.5
"-2.5" is not a valid hex number
htoi(-2.5): -2
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
10
htoi(10): 16
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x9
htoi(0x9): 9
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
g
"g" is not a valid hex number
htoi(g): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-25
htoi(-25): -37
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0x-10 // write -0x10
"0x-10" is not a valid hex number
htoi(0x-10): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-0x10
htoi(-0x10): -16
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+025
htoi(+025): 37
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
oxc // letter 'o' instead of digit '0'
"oxc" is not a valid hex number
Did you mean "0xc"?
Note the difference between 'o' or 'O' and '0' (zero)
htoi(oxc): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xc
htoi(0xc): 12
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+Ox14
"+Ox14" is not a valid hex number
Did you mean "+0x14"?
Note the difference between 'o' or 'O' and '0' (zero)
htoi(+Ox14): 0
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
+0x14
htoi(+0x14): 20
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xa1
htoi(0xa1): 161
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0xaA
htoi(0xaA): 170
/htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
0Xaa
htoi(0Xaa): 170
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
aA
htoi(aA): 170
./htoi
Enter a hex number [+-]?[0x]?[0-9a-f]+ or [+-]?[0X]?[0-9A-F]+
-AA
htoi(-AA): -170
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 20 // max length of octal string (3 octal digits per byte)
int otoi(char octal[]); // octal (string) to decimal (integer)
int main()
{
char s[LENGTH];
printf("Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+\n");
scanf("%s", s);
printf("otoi(%s): %d\n", s, otoi(s));
return 0;
}
int otoi(char octal[]) // octal (string) to decimal (integer)
{
if (octal[0] == '\0') // empty string is not a number
{
printf("\"%s\" is not a valid number\n", octal);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here octal[] is not empty
int i = 0, n = 0, sign = 1;
char temp;
if (octal[i] == '-')
{
sign = -1;
i++; // skip optional sign
}
else if (octal[i] == '+')
{
i++; // skip optional sign
}
if (octal[i] == '0')
{
i++; // skip optional '0'
if (octal[i] == '\0')
{return n;} // return 0; // valid octal number
}
else if (octal[i] == 'o' || octal[i] == 'O')
{
printf("\"%s\" is not a valid octal number\n", octal);
temp = octal[i];
octal[i] = '0';
printf("Did you mean \"%s\"?\n", octal);
octal[i] = temp;
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
else if (octal[i] == '\0')
{
printf("\"%s\" is not a valid octal number\n", octal);
return 0;
}
while (octal[i] != '\0')
{
if (octal[i] >= '0' && octal[i] <= '7')
{n = n*8 + (octal[i]-'0');}
else // not an octal number
{
printf("\"%s\" is not a valid octal number\n", octal);
return sign * n;
}
i++;
}
return sign * n;
}
/*
gcc otoi.c -o otoi
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
0
otoi(0): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+
"+" is not a valid octal number
otoi(+): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-
"-" is not a valid octal number
otoi(-): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
1.2
"1.2" is not a valid octal number
otoi(1.2): 1
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-2.5
"-2.5" is not a valid octal number
otoi(-2.5): -2
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
o1 // letter 'o' instead of digit '0'
"o1" is not a valid octal number
Did you mean "01"?
Note the difference between 'o' or 'O' and '0' (zero)
otoi(o1): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
22
otoi(22): 18
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+022
otoi(+022): 18
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
+0022
otoi(+0022): 18
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-09
"-09" is not a valid octal number
otoi(-09): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
0-7 // write -07
"0-7" is not a valid octal number
otoi(0-7): 0
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-07
otoi(-07): -7
./otoi
Enter an octal number [+-]?0[0-7]* or [+-]?[0-7]+
-7
otoi(-7): -7
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 50 // max length of binary string (8 bits per byte)
int btoi(char []); // binary (string) to decimal (integer)
int main()
{
char s[LENGTH];
printf("Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+\n");
scanf("%s", s);
printf("btoi(%s): %d\n", s, btoi(s));
return 0;
}
int btoi(char bin[]) // binary (string) to decimal (integer)
{
if (bin[0] == '\0') // empty string is not a number
{
printf("\"%s\" is not a valid number\n", bin);
return 0; // should handle the error with an exception as we cannot
} // signal the error here with a returned value
// here bin[] is not empty
int i = 0, n = 0, sign = 1;
char temp;
if (bin[i] == '-') // negative number
{
sign = -1;
i++; // skip optional sign
}
else if (bin[i] == '+') // positive number
{
i++; // skip optional sign
}
if (bin[i] == '0')
{
i++;
if (bin[i] == '\0')
{return n;} // return 0; // valid binary number
if (bin[i] == 'b' || bin[i] == 'B')
{i++;} // skip optional 0b or 0B
}
else if (bin[i] == 'o' || bin[i] == 'O')
{
temp = bin[i];
i++;
if (bin[i] == 'b' || bin[i] == 'B')
{
printf("\"%s\" is not a valid binary number\n", bin);
bin[i-1] = '0';
printf("Did you mean \"%s\"?\n", bin);
bin[i-1] = temp;
printf("Note the difference between 'o' or 'O' and '0' (zero)\n");
return 0;
}
}
if (bin[i] == '\0')
{ // "0b" or "0B" is not a valid binary number
printf("\"%s\" is not a valid binary number\n", bin);
return 0;
}
while (bin[i] != '\0')
{
if (bin[i] == '0' || bin[i] == '1')
{n = n*2 + (bin[i]-'0');}
else // not a binary number
{
printf("\"%s\" is not a valid binary number\n", bin);
return sign * n;
}
i++;
}
return sign * n;
}
/*
gcc btoi.c -o btoi
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+
"+" is not a valid binary number
btoi(+): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-
"-" is not a valid binary number
btoi(-): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+01
btoi(+01): 1
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b
"0b" is not a valid binary number
btoi(0b): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
a
"a" is not a valid binary number
btoi(a): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
123
"123" is not a valid binary number
btoi(123): 1
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-14
"-14" is not a valid binary number
btoi(-14): -1
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
ob1 // letter 'o', not digit '0'
"ob1" is not a valid binary number
Did you mean "0b1"?
Note the difference between 'o' or 'O' and '0' (zero)
btoi(ob1): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b1
btoi(0b1): 1
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0b101
btoi(-0b101): -5
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
11111111
btoi(11111111): 255
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0B100000000
btoi(-0B100000000): -256
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0b+1 // write +0b1
"0b+1" is not a valid binary number
btoi(0b+1): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
+0b1
btoi(+0b1): 1
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
0B-11 // write -0B11
"0B-11" is not a valid binary number
btoi(0B-11): 0
./btoi
Enter a binary number [+-]?(0b)?[01]+ or [+-]?(0B)?[01]+
-0B11
btoi(-0B11): -3
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
// automatic conversions
int main()
{
int c;
printf("Input decimal number: ");
scanf("%d", &c);
printf("Int:\t%d\n", c);
printf("Char:\t%c\n", c);
printf("Octal:\t%o\n", c);
printf("Hex:\t%x\n", c);
return 0;
}
/*
gcc autoconv.c -o autoconv
./autoconv
Input decimal number: 84
Int: 84
Char: T
Octal: 124
Hex: 54
*/