what's wrong in this code?

This is a discussion on what's wrong in this code? within the C Programming forums, part of the General Programming Boards category; I've put a code together, a simple one. It's supposed to search an array of pointers (I initialized it at ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    Question what's wrong in this code?

    I've put a code together, a simple one. It's supposed to search an array of pointers (I initialized it at the beginning).
    each word is defined as any sequence of alphanumeric characters only:
    "okij" - valid word
    "good" - valid word
    "good bye" - not valid word
    "boat." - not valid word

    Now this is what the code is suppoosed to do:
    if a given word is too long (>10) then return 0
    or
    a word is considered correct if found in the array OR if it is a digit, OR if the word is defined properly (sequence of alphanumeric chars) then return 3

    otherwise it is considered incorrect and returns 1

    And it isn't working...why? please help
    Attached Files Attached Files
    • File Type: c ar1.c (799 Bytes, 67 views)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if ( strcmp(word, *dictionary[i]) == 0)
    should be
    >if ( strcmp(word, dictionary[i]) == 0)

    >for (j=0; j<= strlen(*word); j++)
    should be
    >for (j=0; j<= strlen(word); j++)
    but in my opinion, you should store the length of the string in another variable and use that in the for statement. Doing it your way means strlen() is called each time you go round the loop (a performance thing, so your safe to ignore this for now if you wish).

    In here:
    Code:
    for (i=0; i<count; i++)
    {
    	if ( strcmp(word, dictionary[i]) == 0)
    		found++;
    }
    why not just change the found++ bit to return (somevalue);

    Similarly, in here:
    Code:
    for (j=0; j<= strlen(word); j++)
    {
    	if ( isdigit( word[j] ) == 0)
    		notNumber++;
    }
    change notNumber++ to return (somevalue);

    But, hey what's this..... if it's not a number we've returned already, so how can we test if for being an isalnum() string? I think your logic is flawed here.

    I think you should write down what tests you need to do on the string, then reorder them in to a sequence which allows you to perform them one at a time, and return as and when one fails. A couple may need to be combined with an if statement, so be careful.

    In fact, thinking about it, according to your rules, a string is valid if it is full of alpha AND/OR numeric characters, so why not stick with using the isalnum() function.

    But then again, in your program, you have a return value of 2, whereas this isn't mentioned in your criteria.

    Does this help? Or am I babbling again?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: what's wrong in this code?

    Originally posted by HOWY
    Now this is what the code is suppoosed to do:
    if a given word is too long (>10) then return 0
    or
    a word is considered correct if found in the array OR if it is a digit, OR if the word is defined properly (sequence of alphanumeric chars) then return 3

    otherwise it is considered incorrect and returns 1
    OK, so I was probably babbling a little to much on my previous reply, so here's some corrected code that does what you ask.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int check_word(char *word)
    {	
    	int i, len;
    	
    	/* Check length, if bad, return 0 */
    	if (strlen(word) > 10)
    		return 0;
    
    	/* Check dictionary first, if found return 3 */
    	for (i=0; i<count; i++)
    	{	
    		if ( strcmp(word, dictionary[i]) == 0)
    			return 3;
    	}
    
    	len = strlen(word);	
    	
    	/* Now check to see if all characters are alnums.
    		If not, return 1
    	*/
    	for (i=0; i < len; i++)
    	{	
    		if (isalnum(word[i]) == 0)
    			return 1;
    	}
    	
    	/* If we get here, the following is true
    		- word is shorter than 10 in length
    		- word is not in the dictionary
    		- word is made up of all alnum characters
    		Therefore, we return 3
    	*/
    	return 3;
    }
    If you have a large dictionary, I'd suggest swapping the function around a bit to improve efficiency. There's no point in checking a 1 million word dictionary for a 5 character string that is made up of all chars.
    Last edited by Hammer; 06-22-2002 at 07:16 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

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