recursive counting words

This is a discussion on recursive counting words within the C Programming forums, part of the General Programming Boards category; Hi, my function checks how many words are in the string,the iterative version of this function works perfect & it ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    recursive counting words

    Hi, my function checks how many words are in the string,the iterative version of this function works perfect & it was too easy to do it, but with recursive ver. I have some problems - I can't understand where I do mistake
    thank's

    Iterative code
    Code:
    int CountWords(char text[])
    {
    	int i = 0, j = 0, k = 0;
            while(text[i])
    	{
    		if(text[i]!=' ')
    		{
    			if(!k)j++;
    			k=1;
    		}
    		else
    		{
    			k=0;
    		}
    		i++;
    }
    return j;
    }
    Recursive version :

    Code:
    int RecCountWords(char text[])
    {
    	int i = 0;
    	if(text[i] == '/0')
    		return 0;
    	else
    		if(text[i] = ' ')
    		{
    			for(; text[i] = ' ';)
    			{
    				i++;
    			}
    			return (1 + RecCountWords(text + i));
    			
    			
    		}
    		else
    			RecCountWords(text + i);
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by poftornik View Post
    Recursive version :

    Code:
    int RecCountWords(char text[])
    {
    	int i = 0;
    	if(text[i] == '/0')
    		return 0;
    	else
    		if(text[i] = ' ')
    		{
    			for(; text[i] = ' ';)
    			{
    				i++;
    			}
    			return (1 + RecCountWords(text + i));
    			
    			
    		}
    		else
    			RecCountWords(text + i);
    }
    This should cause a seg fault.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    a better way to check white space is with the isspace() function.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kennedy View Post
    This should cause a seg fault.
    There are many errors there.

    1. The nul check should be \0 and not /0
    2. == is not =
    3. The final else doesn't return any value.
    4. Not an error, but why is the incrementing in the loop body instead of the final portion of the for loop?

    They need to compile with warnings on.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 12:45 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 01:03 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. counting words in string
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-30-2002, 05:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21