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;
}