473,769 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R 2 exercise 2-3

Hi~ i've studied C for a few months myself,

and i'd appreciate it if anyone could improve my coding or correct it.

the following is my solution to the K&R exercise 2-3

"Write the function htoi(s), which converts a string of hexademical digits
(including an optional 0x or 0X) into its equivalent integer value.
The allowable digits are 0 through 9, a through f, and A throught F."
//*************** *************** *************** *************** **************

#include <stdio.h>

int isxdigit2(int c)
{
if ( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9') )
return 1;
else
return 0;
}

int tolower2(int c)
{
if (c >= 'A' && c <= 'Z')
return c+32;
else
return c;
}

int power2(int base, int num)
{
int sum;
for (sum = 1; num >0 ; num --)
sum *= base;
return sum;
}

int char_to_num(int c)
{
if (c >= '0' && c <= '9')
{
return c - 48;
}
else
{
return 10 + (tolower2(c) - 'a');
}
}

int htoi(char *c)
{
int i, k, prefix = 0;
size_t sum = 0;

if (c[0] == '0' && tolower2(c[1]) == 'x')
prefix = 1;

for (i = (prefix == 1)? 2:0 ; c[i] ;i++ )
{
if (!isxdigit2(c[i]) )
{
printf("Wrong hexa number\n");
return 0;
}
c[i] = char_to_num(c[i]);
}

for (k = (prefix == 1)? 2 : 0 ; k <= i-1 ; ++k )
{
sum += c[k] * power2(16, i-1-k);
}

return sum;
}

int main()
{
char c[] = "0xAB";
printf("%u", htoi(c));

return 0;
}

//*************** *************** *************** *************** ******

when i change char c[] to char *c in main(),
it shows error, why ??

Thanks..
Nov 14 '05 #1
46 3705
"Herrcho" <he*********@ko rnet.net> wrote in message
news:76******** *************** **@posting.goog le.com...
Hi~ i've studied C for a few months myself,
Good for you.
and i'd appreciate it if anyone could improve my coding or correct it.

the following is my solution to the K&R exercise 2-3
Excellent, finally someone who has actually shown some code! ;-)
Beware, many of my comments below are nits, but it is nice to learn the
good habits now before you'd need to unlearn the wrong ones, like myself.
"Write the function htoi(s), which converts a string of hexademical digits
(including an optional 0x or 0X) into its equivalent integer value.
The allowable digits are 0 through 9, a through f, and A throught F."

//*************** *************** *************** *************** **************
#include <stdio.h>

int isxdigit2(int c)
Identifiers starting with 'is' followed by a lowercase letter are reserved
by the C standard. Use something like is_xdigit2 instead.
By the way, there already is a macro isxdigit, you need to #include
<ctype.h>
{
if ( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9') )

This is not guaranteed to work on non-ASCII systems. The ranges 'a'..'f' and
'A'..'F' do not need to be consecutive. It is guaranteed about '0'..'9',
though.
return 1;
else
return 0;
}
Proper indentation would make your code easier to read.
int tolower2(int c)
{
if (c >= 'A' && c <= 'Z')
return c+32;
else
return c;
}
Same comments as above. BTW, identifiers starting with 'to' are reserved
too.
int power2(int base, int num)
{
int sum;
for (sum = 1; num >0 ; num --)
sum *= base;
return sum;
}

int char_to_num(int c)
{
if (c >= '0' && c <= '9')
{
return c - 48;
Where does the magic number 48 come from? ITYM return c - '0';
}
else
{
return 10 + (tolower2(c) - 'a');
Again, this only works if you are sure that 'a'..'f' is a consecutive set.
}
}

int htoi(char *c)
{
int i, k, prefix = 0;
size_t sum = 0;

if (c[0] == '0' && tolower2(c[1]) == 'x')
prefix = 1;
Are you sure that you have 2 valid characters in c? What happens when you
call htoi("7"), for example?
Try:

if (c[0] && c[0] == '0' && c[1] && (c[1] == 'x' || c[1] == 'X'))
prefix = 1;

It's admittably a bit less elegant but safer. By the way, you could work on
an algorithm without the variable prefix. It's really simple, think about it
a little. All you need is to skip the first 2 characters of c...
for (i = (prefix == 1)? 2:0 ; c[i] ;i++ )
{
if (!isxdigit2(c[i]) )
{
printf("Wrong hexa number\n");
"Hexa" as in "jinxed"? ;-)
return 0;
}
c[i] = char_to_num(c[i]);
}

for (k = (prefix == 1)? 2 : 0 ; k <= i-1 ; ++k )
{
sum += c[k] * power2(16, i-1-k);
}

return sum;
}

int main()
{
char c[] = "0xAB";
printf("%u", htoi(c));
Err, no sir. %u in printf() expects unsigned int, but you provide it with
the result of htoi(), which returns int. This is strictly speaking an
undefined behaviour, although admittably I have yet to see a platform where
it does not work. In any case, use %d instead or change your htoi() to
return unsigned int.
return 0;
}
It looks OK, though overly complicated. You could use already available
macros isxdigit and tolower (both defined in ctype.h), but frankly you
should not even need them. And you certainly should not need your power2().

Think about it. What's 1986 in decimal?
In your algorithm, it's 1*10^3 + 9*10^2 + 8*10^1 + 1*10^0.
How about (((1)*10 + 9)*10 + 8)*10 + 6?

Now try converting it to a C program, for any (hexa)decimal number with any
number of digits.
//*************** *************** *************** *************** ******

when i change char c[] to char *c in main(),
it shows error, why ??
Undefined behaviour. Your htoi() tries to alter the string it is given in
situ. With char c[] in main, the string literal "0xAB" get copied to a local
array c, which is OK. But when you declare c as char *, you pass a pointer
to the string literal to htoi()... oops!
Thanks..


HTH,

Peter
Nov 14 '05 #2
nrk
Herrcho wrote:
Hi~ i've studied C for a few months myself,

and i'd appreciate it if anyone could improve my coding or correct it.

the following is my solution to the K&R exercise 2-3

"Write the function htoi(s), which converts a string of hexademical digits
(including an optional 0x or 0X) into its equivalent integer value.
The allowable digits are 0 through 9, a through f, and A throught F."


First off, not a bad attempt. But the biggest problem is that you've
assumed ASCII character set. Read on for the complete review...
//*************** *************** *************** *************** **************
#include <stdio.h>

#include <string.h> /* 'coz I am gonna use strchr */
int isxdigit2(int c)
{
if ( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0'
&& c <= '9') )
Here you assume 'a'-'f' and 'A'-'F' are in contiguous ascending order in the
character set. This need not be true (yes, even I wasn't happy to know
that). Note however that '0'-'9' are guaranteed to be in contiguous
ascending order by the standard.

So, what do we do? Simple, we keep a string with valid hex-alphabets and
see if the character to be checked occurs within that string:

static const char *hexalpha = "abcdefABCD EF";

if ( (c >= '0' && c <= '9') || (c && strchr(hexalpha , c)) )
return 1;
else
return 0;
}

Alternately, you could just use the isxdigit standard function, but that's
way less fun :-)
int tolower2(int c)
{
if (c >= 'A' && c <= 'Z')
return c+32;
Ouch!! Once again, you've not only assumed that 'A'-'Z' are contiguous
ascending, but also assumed that 'A'+32 == 'a', which need not be true at
all. You could either use the standard tolower, or:
static const char *uppercase = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
static const char *lowercase = "abcdefghijklmn opqrstuvwxyz";

char *cptr = strchr(uppercas e, c);
if ( cptr ) return lowercase[cptr - uppercase];
return c;
else
return c;
}

int power2(int base, int num)
{
int sum;
for (sum = 1; num >0 ; num --)
sum *= base;
return sum;
}

Hmmm... way too complicated... see below for why this isn't needed.
int char_to_num(int c)
{
if (c >= '0' && c <= '9')
{
return c - 48;
Ouch!! Here you assumed '0' == 48, which need not be true. Why not:
return c - '0';
}
else
{
return 10 + (tolower2(c) - 'a');
Again, 'a'-'f' need not be contiguous ascending in the character set. What
you can do instead is:
static const char *hexalpha = "abcdef";
return 10 + (strchr(hexalph a, tolower2(c)) - hexalpha);
}
}

int htoi(char *c)
Make that:
unsigned int htoi(char *c)
{
int i, k, prefix = 0;
size_t sum = 0; unsigned int sum;
if (c[0] == '0' && tolower2(c[1]) == 'x')
prefix = 1;

for (i = (prefix == 1)? 2:0 ; c[i] ;i++ )
{
if (!isxdigit2(c[i]) )
{
printf("Wrong hexa number\n");
return 0;
}
c[i] = char_to_num(c[i]);
}

for (k = (prefix == 1)? 2 : 0 ; k <= i-1 ; ++k )
{
sum += c[k] * power2(16, i-1-k);
}

return sum;
}

You can simplify matters quite a bit if you recognize that proceeding
left-to-right in the string is the best thing that you can do. Simply
multiply whatever you have by 16 and add the next number in line, and voila
you have the correct conversion at the end.

if ( c[0] == '0' && (c[1] == 'x' || c[1] == 'X') )
c += 2;
while ( *c && isxdigit2(*c) ) {
sum = (sum * 16) + char_to_num(*c) ;
++c;
}
if ( *c ) {
fprintf(stderr, "Invalid hex digit %c in string\n", *c);
sum = 0;
}
return sum;
int main()
{
char c[] = "0xAB";
printf("%u", htoi(c));

return 0;
}

//*************** *************** *************** *************** ******

when i change char c[] to char *c in main(),
it shows error, why ??

Who shows what error?

-nrk.
Thanks..


--
Remove devnull for email
Nov 14 '05 #3
nrk
Peter Pichler wrote:
"Herrcho" <he*********@ko rnet.net> wrote in message
news:76******** *************** **@posting.goog le.com...
Hi~ i've studied C for a few months myself,
Good for you.
and i'd appreciate it if anyone could improve my coding or correct it.

the following is my solution to the K&R exercise 2-3


Excellent, finally someone who has actually shown some code! ;-)
Beware, many of my comments below are nits, but it is nice to learn the
good habits now before you'd need to unlearn the wrong ones, like myself.
"Write the function htoi(s), which converts a string of hexademical
digits (including an optional 0x or 0X) into its equivalent integer
value. The allowable digits are 0 through 9, a through f, and A throught
F."

//*************** *************** *************** *************** **************

#include <stdio.h>

int isxdigit2(int c)


Identifiers starting with 'is' followed by a lowercase letter are reserved
by the C standard. Use something like is_xdigit2 instead.
By the way, there already is a macro isxdigit, you need to #include
<ctype.h>
{
if ( (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c
<=

'9') )

This is not guaranteed to work on non-ASCII systems. The ranges 'a'..'f'
and 'A'..'F' do not need to be consecutive. It is guaranteed about
'0'..'9', though.
return 1;
else
return 0;
}


Proper indentation would make your code easier to read.


The indentation's ok, but he/she used tabs instead of spaces. To OP: You
should use spaces to indent code that you post on the usenet.
int tolower2(int c)
{
if (c >= 'A' && c <= 'Z')
return c+32;
else
return c;
}


Same comments as above. BTW, identifiers starting with 'to' are reserved
too.
int power2(int base, int num)
{
int sum;
for (sum = 1; num >0 ; num --)
sum *= base;
return sum;
}

int char_to_num(int c)
{
if (c >= '0' && c <= '9')
{
return c - 48;


Where does the magic number 48 come from? ITYM return c - '0';
}
else
{
return 10 + (tolower2(c) - 'a');


Again, this only works if you are sure that 'a'..'f' is a consecutive set.
}
}

int htoi(char *c)
{
int i, k, prefix = 0;
size_t sum = 0;

if (c[0] == '0' && tolower2(c[1]) == 'x')
prefix = 1;


Are you sure that you have 2 valid characters in c? What happens when you
call htoi("7"), for example?


Any non-empty string will have valid values in c[0] and c[1]. Also, the
standard guarantees that '\0' != '0' :-)
Try:

if (c[0] && c[0] == '0' && c[1] && (c[1] == 'x' || c[1] == 'X'))
prefix = 1;

It's admittably a bit less elegant but safer. By the way, you could work
on an algorithm without the variable prefix. It's really simple, think
about it a little. All you need is to skip the first 2 characters of c...
for (i = (prefix == 1)? 2:0 ; c[i] ;i++ )
{
if (!isxdigit2(c[i]) )
{
printf("Wrong hexa number\n");


"Hexa" as in "jinxed"? ;-)
return 0;
}
c[i] = char_to_num(c[i]);
}

for (k = (prefix == 1)? 2 : 0 ; k <= i-1 ; ++k )
{
sum += c[k] * power2(16, i-1-k);
}

return sum;
}

int main()
{
char c[] = "0xAB";
printf("%u", htoi(c));


Err, no sir. %u in printf() expects unsigned int, but you provide it with
the result of htoi(), which returns int. This is strictly speaking an
undefined behaviour, although admittably I have yet to see a platform
where it does not work. In any case, use %d instead or change your htoi()
to return unsigned int.
return 0;
}


It looks OK, though overly complicated. You could use already available
macros isxdigit and tolower (both defined in ctype.h), but frankly you
should not even need them. And you certainly should not need your
power2().

Think about it. What's 1986 in decimal?
In your algorithm, it's 1*10^3 + 9*10^2 + 8*10^1 + 1*10^0.
How about (((1)*10 + 9)*10 + 8)*10 + 6?

Now try converting it to a C program, for any (hexa)decimal number with
any number of digits.
//*************** *************** *************** *************** ******

when i change char c[] to char *c in main(),
it shows error, why ??


Undefined behaviour. Your htoi() tries to alter the string it is given in
situ. With char c[] in main, the string literal "0xAB" get copied to a
local array c, which is OK. But when you declare c as char *, you pass a
pointer to the string literal to htoi()... oops!


Waah... I missed that one, didn't I? Tricksy little hobbitses...

-nrk.
Thanks..


HTH,

Peter


--
Remove devnull for email
Nov 14 '05 #4
How about this one?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAX_STR 80

void
reverse ( char * str )
{
char *beg, *end;

beg = str;
end = str + strlen ( str ) - 1;

while ( beg < end )
{
*beg ^= *end ^= *beg ^= *end;
beg++;
end--;
}
return;
}

int
to_num ( char dig )
{
if ( !isxdigit ( dig ) )
{
fprintf ( stderr, "Not a hexadecimal number" );
exit ( EXIT_FAILURE );
}

if ( isalpha ( dig ) )
return tolower ( dig ) - 'a' + 10;
else
return dig - '0';
}
int
main ( void )
{
char str[MAX_STR];
long dec = 0, exp = 0;
char *ptr;

printf ( "Enter a hexadecimal number: " ); if ( !fgets ( str, MAX_STR,
stdin ) ) return EXIT_FAILURE; /* discard "0x" or "0X" */
if ( str[0] == '0' )
if ( tolower ( str[1] ) == 'x' )
memmove ( str, str+2, 3 );
puts ( str );

/* use function reverse(s) of exercise 1-19.c */
reverse ( str );
ptr = str;

while ( *ptr )
{
dec = dec + to_num ( *ptr ) * pow ( 16, exp );
exp++;
ptr++;
}

printf ( "Decimal: %ld\n", dec );
return EXIT_SUCCESS;
}
Nov 14 '05 #5
"nrk" <ra*********@de vnull.verizon.n et> wrote:
Peter Pichler wrote:
"Herrcho" <he*********@ko rnet.net> wrote:

int htoi(char *c)
{
int i, k, prefix = 0;
size_t sum = 0;

if (c[0] == '0' && tolower2(c[1]) == 'x')
prefix = 1;


Are you sure that you have 2 valid characters in c? What happens when you call htoi("7"), for example?


Any non-empty string will have valid values in c[0] and c[1]. Also, the
standard guarantees that '\0' != '0' :-)


Err... yes, but... mumbleohbuggerh umblegrumble... OK, I concede ;-)
Nov 14 '05 #6
nrk
Vijay Kumar R Zanvar wrote:
How about this one?
Did you even care to test it once? Apart from being needlessly complicated,
reliant on a contiguous ascending alphabet character set, unnecessarily
using real arithmetic, causing gratuituous undefined behavior, it is also
hopelessly broken because fgets may leave '\n' in your buffer.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAX_STR 80

void
reverse ( char * str )
{
char *beg, *end;

beg = str;
end = str + strlen ( str ) - 1;
Disaster waiting to happen when str == "".

while ( beg < end )
{
*beg ^= *end ^= *beg ^= *end;
beg++;
end--;
Needlessly cute. Try to use the language correctly before being "leet".
Whatever in the world is wrong with?:
*beg++ = *end--;
Do you object to it because it is more readable and demonstrably correct in
this context? Do you object to it because it is likely more efficient?
}
return;
}

int
to_num ( char dig )
{
if ( !isxdigit ( dig ) )
{
fprintf ( stderr, "Not a hexadecimal number" );
Missing '\n'.
exit ( EXIT_FAILURE );
}

if ( isalpha ( dig ) )
return tolower ( dig ) - 'a' + 10;
What if 'a' > 'b'? What if 'b' == 'a' + 3?
else
return dig - '0';
}
int
main ( void )
{
char str[MAX_STR];
long dec = 0, exp = 0;
char *ptr;

printf ( "Enter a hexadecimal number: " ); if ( !fgets ( str,
MAX_STR,
stdin ) ) return EXIT_FAILURE; /* discard "0x" or "0X" */
if ( str[0] == '0' )
if ( tolower ( str[1] ) == 'x' )
memmove ( str, str+2, 3 );
Oh Joy!! Even more undefined behavior. Who told you that str+3 and str+4
will contain something valid? If str == "0x" or str == "0x\n", you end up
touching uninitialized memory.
puts ( str );

/* use function reverse(s) of exercise 1-19.c */
reverse ( str );
ptr = str;

while ( *ptr )
{
dec = dec + to_num ( *ptr ) * pow ( 16, exp );
exp++;
ptr++;
}

printf ( "Decimal: %ld\n", dec );
return EXIT_SUCCESS;
}


Try to write unoptimised, "unleet", correct code before getting fancy. Try
to atleast test your code once before posting it [this one doesn't work for
one single valid case, except on stdin redirection and a non-standard file
with no ending newline that contains a valid hex number].

-nrk.

--
Remove devnull for email
Nov 14 '05 #7
nrk
nrk wrote:
Vijay Kumar R Zanvar wrote:
How about this one?


Did you even care to test it once? Apart from being needlessly
complicated, reliant on a contiguous ascending alphabet character set,
unnecessarily using real arithmetic, causing gratuituous undefined
behavior, it is also hopelessly broken because fgets may leave '\n' in
your buffer.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAX_STR 80

void
reverse ( char * str )
{
char *beg, *end;

beg = str;
end = str + strlen ( str ) - 1;


Disaster waiting to happen when str == "".

while ( beg < end )
{
*beg ^= *end ^= *beg ^= *end;
beg++;
end--;


Needlessly cute. Try to use the language correctly before being "leet".
Whatever in the world is wrong with?:
*beg++ = *end--;
Do you object to it because it is more readable and demonstrably correct
in this context? Do you object to it because it is likely more efficient?


Got carried away there. You should object because it is demonstrably
incorrect :-) Obviously you need to swap the two values with a temporary
intermediary:

char temp = *beg;
*beg++ = *end;
*end-- = temp;

Taking a dose of my own medicine, I should've tested the darn thing before
posting.

Also, that memmove breaks your program for most valid inputs starting with
0x.

-nrk.
Nov 14 '05 #8
nrk wrote:

nrk wrote:
Vijay Kumar R Zanvar wrote:
How about this one?


Did you even care to test it once? Apart from being needlessly
complicated, reliant on a contiguous ascending alphabet character set,
unnecessarily using real arithmetic, causing gratuituous undefined
behavior, it is also hopelessly broken because fgets may leave '\n' in
your buffer.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAX_STR 80

void
reverse ( char * str )
{
char *beg, *end;

beg = str;
end = str + strlen ( str ) - 1;


Disaster waiting to happen when str == "".

while ( beg < end )
{
*beg ^= *end ^= *beg ^= *end;
beg++;
end--;


Needlessly cute. Try to use the language correctly before being "leet".
Whatever in the world is wrong with?:
*beg++ = *end--;
Do you object to it because it is more readable and demonstrably correct
in this context? Do you object to it because it is likely more efficient?


Got carried away there. You should object because it is demonstrably
incorrect :-) Obviously you need to swap the two values with a temporary
intermediary:

char temp = *beg;
*beg++ = *end;
*end-- = temp;

Taking a dose of my own medicine, I should've tested the darn thing before
posting.

Also, that memmove breaks your program for most valid inputs starting with
0x.

-nrk.


Try this..

/*
Program: htol.c
Author: Joe Wright <jo********@ear thlink.net>

Convert a hexadecimal char to its numeric equivalent.
Accommodate ASCII and EBCDIC. Maybe others.
*/

#include <stdio.h>

typedef unsigned char uchar;
typedef unsigned long ulong;

/*
We will examine a printing hex character and determine its bin value.
Of course the numerics will translate directly. The alphas are a
special case as both 'a' and 'A' will evaluate to 10.
Input must be int within the ranges '0'..'9', 'A'..'F' and 'a'..'f'.

Some assumptions:

That '0',,'9', 'A'..'F' and 'a'..'f' are contiguous in the set.
'0'..'9' is guaranteed but the others are not. Oh well, they are
contiguous in ASCII and EBCDIC.

*/

/* Returns a value 0..15 for hex digits or -1 on failure. */

int h2b(int h) {
int i, b = -1; /* Error code */
if ((i = h - '0') >= 0 && i < 10)
b = i;
else if ((i = h - 'a') >= 0 && i < 6)
b = i + 10;
else if ((i = h - 'A') >= 0 && i < 6)
b = i + 10;
return b;
}

ulong h2l(char *h) {
ulong l = 0;
int c;
while ((c = *h++) && (c == '0' || c == 'x' || c == 'X'))
;
if (c)
do {
l = l * 16 + h2b(c);
} while((c = *h++));
return l;
}

int main(int argc, char *argv[]) {
ulong ans = 0;
if (argc > 1)
ans = h2l(argv[1]);
printf("\t%lu\n ", ans);
return 0;
}

--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
Joe Wright wrote:
.... snip ...
/*
We will examine a printing hex character and determine its bin value.
Of course the numerics will translate directly. The alphas are a
special case as both 'a' and 'A' will evaluate to 10.
Input must be int within the ranges '0'..'9', 'A'..'F' and 'a'..'f'.

Some assumptions:

That '0',,'9', 'A'..'F' and 'a'..'f' are contiguous in the set.
'0'..'9' is guaranteed but the others are not. Oh well, they are
contiguous in ASCII and EBCDIC.
*/

/* Returns a value 0..15 for hex digits or -1 on failure. */
int h2b(int h) {


Why make any assumptions?

/* also useful for reverse conversions */
static char[] hexchars = "0123456789abcd efABCDEF";

/* Returns a value 0..15 for hex digits or -1 on failure. */
int h2b(int h)
{
char * s;

if (NULL == (s = strchr(hexchars , h))) h = -1;
else {
h = s - hexchars;
if (h > 15) h = h - 6;
}
if (h > 15) h = -1; /* Exercise - why this */
return h;
}

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3805
by: leonard greeff | last post by:
I want to know the correct way to answer exercise 1-11 of K&R. The only bug that I can find is that nw counts one to many words. (if there are 8 words, nw will be 9) Am I correct aor is there more to it leonard
12
2177
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which give me strange behavior. Here is the code I've written: /****************************************************************************** * K&R2 Exercise 1-9 * Write a program to copy its input to its output, replacing strings of blanks * with a...
12
2336
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5; while(c != EOF ) {
8
3087
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as follows: "Write a program to 'fold' long input lines into two or more shorter
16
2280
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of blanks as a single one */ int main() { int c;
19
2407
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
5
2919
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any compile-error BUT it has a semantic BUG: what i WANT: I want it to produce a "horizontal histogram" which tells how many characters were in the 1st word, how many characters were in the second word by writing equal number of stars, *, at the...
9
3043
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do. I am not even done with the first chapter and I am already ripping out my hair trying to get the exercises completed. It is driving me crazy and making me very very angry. Here is an example of what I am talking about: Chapter 1.6 Arrays...
88
3788
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without invoking undefined behaviour triggered by overflow? Remember that the constants in limits.h cannot be used.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.