Saturday, August 21, 2010

Blank char counter in C

Programmer Question

I am trying to create a small function that returns the number of spaces in a Char*
variable using the C language.



Let's say I have this string :"hello hello hello". I want the function to return 2.
This is the code I have so far:



int blankcounter(char* pline)
{
int i=0;
int counter = 0;
while (pline[i] != '\0')
{
if (pline[i++] ==' ')
counter++;
}
return counter;
}


the source that i am reading from is a txt file and 1 correction that i have to add is that the code that i posted indeed works but got 1 downside: if for example i want to read : "hello whats up "i want my function to be able to return 2 but it returns 3 because of the space that appears just after the wordup do u have any suggestion for me so it will return 2 ?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails