Thread: parsing and identifying input

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Question parsing and identifying input

    i need to write a program that inputs a string then parses it for words then identifies if it is a number or not.
    i used strtok to parse the string then i need to know how to identify the characters to see if they are numbers or not assuming that the parsed string has at most 4 characters.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Could you post the code of your attempt? And perhaps an example expected input/output?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    input output

    i've done this so far but it either returns all a number or all not a number no matter what input i put in.
    Example input output:
    Enter string : 1 3 aa +
    Output :
    1 Number
    3 Number
    aa Not a number
    + Operator
    Code:
    # include <stdio.h>
    # include <string.h>
    # include <ctype.h>
    # define STRING 100
    # define WORD 4
    
    int val(char tokens[])
    {
    	int i=0;
    	
    	while () 
    	{
    		if(!isdigit(tokens[i]))
    		{
    			return 0;
    		}
    		i++;
    	}
    		
    	return 1; 
    
    }
    
    int main(void)
    {
    	char charStr[STRING];
    	char token[LEN];
    	char *tokenPtr = token;
    	int i;
    	
    	printf("Enter string : ");
    		
    	if(fgets(charStr, STRING, stdin) != NULL)
    	{
    		tokenPtr = strtok(charStr, " ");
    		
    		while(tokenPtr != NULL)
    		{
    			if(val(token) == 1)	
    				printf("%s  Number\n", tokenPtr);
    			
    			else
    				printf("%s  Not a number\n", tokenPtr);
    			
    			tokenPtr = strtok(NULL, " ");
    		}
    	}
    	return 0;
    }
    Last edited by Salem; 08-13-2005 at 11:59 AM. Reason: tagging

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Please use [code][/code]Tags
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use the result from strtok:
    Code:
    if ( val(tokenPtr) == 1 )
    You also may want to #define LEN [edit]actually, just delete the token declaration[/edit] and put a condition in your while loop.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing multiple pages of dynamically created Websites
    By Quarlash in forum C Programming
    Replies: 11
    Last Post: 07-07-2009, 04:21 PM