Re: Counting total space,tab,new line in a string.
From: Zoran Cutura (zoran.cutura_at_daimlerchrysler.com)
Date: 04/01/04
- Next message: Richard Bos: "Re: Reading program output from stdin"
- Previous message: Marky C: "strtod - Dynamic Memory?"
- In reply to: dam_fool_2003_at_yahoo.com: "Re: Counting total space,tab,new line in a string."
- Next in thread: Malcolm: "Re: Counting total space,tab,new line in a string."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 1 Apr 2004 13:30:58 GMT
dam_fool_2003@yahoo.com wrote:
> "Malcolm" <malcolm@55bank.freeserve.co.uk> wrote in message news:<c4f4gq$trk$1@newsg4.svr.pol.co.uk>...
> <header files included>
> unsigned int space_count(const char *chars)
> {
> const char *temp;
> int i=0;
> unsigned char a;
> temp = malloc(sizeof temp);
> if(!temp)
> {
> printf("mem alloc error\n");
> exit(EXIT_FAILURE);
> }
> temp = chars;
That is the foolish, foolish, foolish. Why in hell would you allocate
memory, just to make the only reference to that memory point somewhere
else in the very next step. What did you actually want to do?
Did you probably mean to copythe string from chars to temp? Why would
you want to do this at all? Can't you simply count the white space in
the string passed with the pointer chars? To answer it, you can and you
should avoid allocating memory here (as almost everywhere) and also
avoid copying data if it is ont neccessary.
You can get lost of the whole malloc() stuff above, because your current
code doesn't even use it, it just introduces memory leaks with every
call to this function.
> while(*temp != '\0')
> {
> a = *temp;
> if(a++ == 0x20) /*ascii value of space. Yet to check for tab,enter
> */
Why don't you simply use the ' ' character instead of guessing what code
it has in whatever environment you are running the program? The compiler
will magically insert the correct value for you.
Also, you're trying to read a character from the string pointed to by
temp (which will always be the same by the way, 'cause you forgot to
move the pointer thru the string), but why on earth would you have to
increase the value of that character?
Especially, when the value of a is never used again after it has been
increased. While running thru the loop it will be reinitilized to the
first character in the temp-string.
> {
> i++;
> return i;
> }
>
> }
> return 0;
> }
Let me write this function how you probably meant to write it:
long space_count(const char *chars)
{
long count = 0;
while(*chars != '\0') {
if(*chars == ' ')
count++;
}
return count;
}
This function only counts spaces, but I think your original question
was to count all whitespace. So you have to define what you want to
count as whitspace and what not. On the first sight, I thought your
original code (from the first post) was ok but had only a few errors.
I would stick to using isspace because it does a good job
t finding out what is space an what isn't for you.
> int main(void)
> {
> const char *data = "dam fool 2003";
> unsigned int i = space_count(data);
> printf("%d\n",i);
> return 0;
> }
>
> The above did not succeeded. The program simply stayed as if it is
> asking for a standard input.
see above.
-- Z (Zoran.Cutura@daimlerchrysler.com) "LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
- Next message: Richard Bos: "Re: Reading program output from stdin"
- Previous message: Marky C: "strtod - Dynamic Memory?"
- In reply to: dam_fool_2003_at_yahoo.com: "Re: Counting total space,tab,new line in a string."
- Next in thread: Malcolm: "Re: Counting total space,tab,new line in a string."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|