Thread: problem with pointer to char

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    problem with pointer to char

    i have a program where user enter a float val(123.45) & this is stored as string. i use strtok to break it into 2 parts. period is the delimiter. now i have to print the value in words that is One hundred and twenty three.

    my question is as strtok returns char * how to traverse through each character(1 then 2 then 3) or how to copy value returned by strtok in a array & then traverse through it? any help will be apreciated greatly!! is there any alternative to this.

    i tried converting into int but then how to traverse is the problem.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Show us the code you have from the problem.
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    ok...... here it is....if i can figure out how to go thru each character then i can complete this prog. thanks for looking!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX    30	
    #define SIZE   10
    
    #define SPERIOD	"."
    #define DECIMAL	".00"
    #define SPACE	" "
    
    #define PERIOD    '.'
    #define ZERO        '0'
    
    #define ONE	'1'
    #define TWO	'2'
    #define THREE	'3'
    #define FOUR	'4'
    #define FIVE	'5'
    #define SIX	'6'
    #define SEVEN	'7'
    #define EIGHT	'8'
    #define NINE	'9'
    
    
    void checkValue(char []);
    void convertToInt(char *, char *, long *, long *);
    void checkNumbers(long *,long *);
    void convertToString(long *,long *, char *, char *);
    void getLengthNumber(char *, int *);
    void printOneWord(char *);
    
    
    int main(void)
    {
    	char value[MAX];
    	char *pnumber;
    	char *pdecimal;
    	char *pfind;
    	char *pnext;
    	char *pstart;
    
    	long num;
    	long deci;
    	int length;
    	
    
    	printf("Enter a float value : ");
    	fgets(value, MAX, stdin);
    
    	pfind = strchr(value, '\n');
    	if(pfind)
    	{
    		*pfind = '\0';
    	}
    	checkValue(value);
    	printf("%s is digit\n", value);
    
    	if(strchr(value, PERIOD) == NULL)
    	{
    		strcat(value, DECIMAL);
    		printf("value is %s\n", value);
    	}
    	
    	pnumber = strtok(value, SPERIOD);
    	pdecimal = strtok(NULL, SPACE);
    	printf("pnumber is %s\n", pnumber);
    	printf("pdecimal is %s\n", pdecimal);
    
    	convertToInt(pnumber, pdecimal, &num, &deci);
    	printf("num is %ld\n", num);
    	printf("deci is %ld\n", deci);
    
    	checkNumbers(&num, &deci);
    	printf("num is %ld\n", num);
    	printf("deci is %ld\n", deci);
    	
    	convertToString(&num, &deci, pnumber, pdecimal);
    	printf("pnumber is %s\n", pnumber);
    	printf("pdecimal is %s\n", pdecimal);
    
    	getLengthNumber(pnumber, &length);
    	printf("length of pnumber is %d\n", length);
    	
    	while(length != 0)
    	{
    		pstart = number;//assigns the whole num 
    		switch(length)
    		{
    			case 1:
    				printOneWord(pstart);
    				break;
    		}
    		pstart++;
    		length--;
    	}
    
    	printf("and %s/100 val\n\n", pdecimal);
    	return 1;
    }
    
    void checkValue(char value[])
    {
    	char *pstart1;
    	char *pfind1;
    
    	pstart1 = value;
    	while(*pstart1 != '\0')
    	{
    		if(isdigit(*pstart1))
    		{
    			printf("%c is digit\n", *pstart1);
    			*pstart1++;
    		}
    		else
    		{
    			if(*pstart1 == PERIOD)
    			{
    				printf("found . go to pnext\n");
    				*pstart1++;
    				continue;
    			}
    			printf("%c is not digit.re-enter : ", *pstart1);
    
    			fgets(value, MAX, stdin);
    			pfind1 = strchr(value, '\n');
    			if(pfind1)
    				*pfind1 = '\0';
    			pstart1 = value;
    		}
    	}
    	return;
    }
    
    
    void convertToInt(char *pnumber, char *pdecimal, long *num, long *deci)
    {
    	char *end;
    
    	*num = strtol(pnumber, &end, 10);
    	*deci = strtol(pdecimal, &end, 10);
    
    	return;
    }
    
    void checkNumbers(long *num, long *deci)
    {
    	if( (*num <= 0) && (*deci <= 0) )
    	{
    		printf("Invalid numbers. Try again\n");
    		exit(1);
    	}
    	else if( (*num <= 0) && (*deci > 0) )
    	{
    		printf("%ld/100 value\n", *deci);
    		exit(1);
    	}
    
    	return;
    }
    
    void convertToString(long *num, long *deci, char *pnumber, char *pdecimal)
    {
    	sprintf(pnumber, "%ld", *num);
    	sprintf(pdecimal, "%ld", *deci);
    	
    	return;
    }
    
    void getLengthNumber(char *pnumber, int *length)
    {
    	*length = strlen(pnumber);
    }
    
    void printOneWord(char *pstart)
    {
    	switch(*pstart)
    	{
    		case ONE:
    			printf("One ");
    			break;
    	}
    	return;
    }

  4. #4
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    Post maybe you can do like this....

    void printOneWord(char *pstart)
    {
    int i;
    for(i=0;i<strlen(pstart);i++)
    {
    switch(pstart[i])
    {
    case ONE:
    printf("One ");
    break;
    case TWO:
    printf("TWO ");
    break;
    ........
    }
    }
    return;
    }

    others you can think yourself;
    there is angel,who love to see u.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Could do something like this


    Code:
    char    IntWordArray[10][64] ={"zero","one",........,"ten"};
    char    sBuffer[128]='\0';
    double  fInput=0,fTemp=0;
    int         =0, iTenIndex=0, iUnitIndex=0;
    
    iHundredIndex = fInput/100;
    if(iHundredIndex>10)
         return FALSE
    else
    {
           fTemp=fInput-(iHundredIndex*100);
           iTenIndex=fTemp/10;
           if(iTenIndex>10)
                   return FALSE
           else....................
    }
    //get user input as fInput
    sprintf(sBuffer,"Number %g is %s hundreds, %s tens and %s units." , 
               fInput , IntWordArray[iHundredIndex] , IntWordArray[iTenIndex] , IntWordArray[iUnitIndex]);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-24-2002, 11:49 PM