Order of strings

Discussion in 'C' started by gavin_55, Apr 23, 2007.

  1. gavin_55

    gavin_55 New Member

    Joined:
    Apr 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi there, i have a small program in C++ that counts how many words are in a sentence. I understand the steps it takes to do this but am having difficulty get those steps in code.

    i then need to discard all leading zero's.
    find a character using isalpha()
    increment counter.
    find next zero and start over.

    This is what i have so far.

    Code:
    int getWords (char *ptr2)//FUNCTION
    {
    	int sum = 0;
    	bool check = false;//FLAG
    	while (!check)//WHILE LOOP
    		for (int count = 0; count < LENGTH; count++)//COUNTER
    		{
    			if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'S
    			{
    				//DO NOTHING
    			}
    			if (isalpha(ptr2[count]))//FIND CHARACTER
    			{
    				sum++; //BEGINNING OF A WORD, START COUNTER
    				for (int num = count; num < LENGTH; num++)
    				{
    					//FIND NEXT SPACE
    					if (!isalpha(ptr2[num]))
    					{
    						check = false;
    						count = num;
    					}
    					else 
    						check = true;
    				}
    			}
    		}
    			
    		return sum;
    }
     
    Last edited by a moderator: Apr 23, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Please read the "Before you make a query" thread, so we don't have to look at ugly, unformatted code in order to help you.
     
  3. gavin_55

    gavin_55 New Member

    Joined:
    Apr 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I'm sorry, i didn't know i could tag them like this.
    Here is my code, its about as clean as it gets for me. I apologise again.

    Code:
    int getWords (char *ptr2)//FUNCTION
    {
    	int sum = 0;
    	bool check = false;//FLAG
    	while (!check)//WHILE LOOP
    		for (int count = 0; count < LENGTH; count++)//COUNTER
    		{
    			if ((!isalpha(ptr2[count])) && (sum == 0))//FIND LEADING ZERO'S
    			{
    				//DO NOTHING
    			}
    			if (isalpha(ptr2[count]))//FIND CHARACTER
    			{
    				sum++; //BEGINNING OF A WORD, START COUNTER
    				for (int num = count; num < LENGTH; num++)
    				{
    					//FIND NEXT SPACE
    					if (!isalpha(ptr2[num]))
    					{
    						check = false;
    						count = num;
    					}
    					else 
    						check = true;
    				}
    			}
    		}
    			
    		return sum;
    }
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    [comment]Its just that I edited and you posted.[/comment]
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    First of all, silly or obvious comments are worthless.
    Code:
    int getWords (char *ptr2)//FUNCTION [COLOR=Red]no kidding![/COLOR]
    while (!check)//WHILE LOOP [COLOR=Red]really![/COLOR]
    
    Secondly, the use of things which have not been clearly shown may suggest that there is an uninitialized variable, when in fact everything might be fine. My crystal is in the shop for 'ball' joints.
    Code:
     count < LENGTH [COLOR=Red]what, exactly, is LENGTH, and from whence does it come?[/COLOR]
    
    Thirdly, erroneous comments are as bad as useless comments. You imply that words are separated by zeroes. Perhaps you mean non-alphabetic characters?
    Code:
    if ((!isalpha(ptr2[count])) && (sum == 0[COLOR=Red]))//FIND LEADING ZERO'S[/COLOR]
    
    and
    Code:
                        [COLOR=Red]//FIND NEXT SPACE[/COLOR]
                        if (!isalpha(ptr2[num]))
                        {
                            check = false;
                            count = num;
                        }
    
    perhaps you meant "find next non-alphabetic character"?

    Further, inappropriately named functions are misleading.
    Code:
    int getWords (char *ptr2)//FUNCTION
    
    Isn't this really "getWordCount"??? "getWords" would return the words.

    In any event, you should pass the function a string, as in the C definition of string, so that the maximum length to work with can be determined by strlen or by the occurrence of a nul character, OR (this is C++, right, says "bool"?) use the C++ string class, which carries the length as a member.

    Here's an untested snippet, suitable for a C string, you might consider.
    Code:
    while (*ptr && !isalpha (ptr++);  [COLOR=Red]Note the semicolon; this loop does nothing but find alpha or end.[/COLOR]
    if (*ptr) count++; Word found, since not pointing at nul.
    while (*ptr && isalpha (ptr++));  Nothing but find end of alpha (thus, one word)
    
    Wash, rinse, and repeat until nul found, return the count.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice