Sunday, November 25, 2018

C code review convert digit representation to equivalent word representation

This C code shows how to convert digit representation into equivalent word representation.And code is helpful while user is in between digit manipulation task.

code:

//Tested OK with Code::Blocks IDE 17.12 & Cygwin+GCC tools.
//Program to convert digit representation to equivalent word representation

#include<stdio.h>

struct std
{
    int dig;
    char wrd[20];
};

struct std
    fig[]= {{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},
    {7,"seven"},{8,"eight"},{9,"nine"},{10,"ten"},{11,"eleven"},{12,"twelve"},
    {13,"thirteen"},
    {14,"fourteen"},{15,"fifteen"},{16,"sixteen"},{17,"seventeen"},{18,"eighteen"},{19,"nineteen"},
    {20,"twenty"},{30,"thirty"},{40,"fourty"},{50,"fifty"},{60,"sixty"},{70,"seventy"},{80,"eighty"},
    {90,"ninety"}
};

int num1[5],i;

void op0(long num)
{
    for(i=0; i<20; i++)
        if(fig[i].dig==num)
            printf("%s",fig[i].wrd);
}

void op1(long num)
{
    //num1=num;


    num1[0]=num/10;
    num1[0]=num1[0]*10;
    num1[1]=num%10;
    for(i=0; i<27; i++)
    {
        if(fig[i].dig==num1[0])
            printf("%s ",fig[i].wrd);
    }
    for(i=0; i<20; i++)
    {
        if(fig[i].dig==num1[1])
            printf("%s",fig[i].wrd);
    }

}

void op2(long num)
{
    int k,j=num%100;
    k=num/100;
    for(i=0; i<10; i++)
    {
        if(fig[i].dig==k)
        {
            printf("%s hundred ",fig[i].wrd);
            if(j>0&&j<20)
                op0(num%100);
            else
                op1(num%100);
        }
    }
}

void op3(long num)
{
    int k,j=num%1000;
    k=num/1000;
    for(i=0; i<10; i++)
    {
        if(fig[i].dig==k)
        {
            printf("%s thousand ",fig[i].wrd);
            if(j>=1&&j<10)
                op0(num%1000);
            else if(j>10&&j<100)
                op1(num%1000);
            else
                op2(num%1000);
        }
    }
}

void op4(long num)
{
    int k=num/1000,j;
    j=num%1000;
    if(k<=20)
    {
        op0(num/1000);
        printf(" thousand ");
        if(j>0&&j<10)
            op0(num%1000);
        if(j>10&&j<100)
            op1(num%1000);
        else
            op2(num%1000);
    }
    if((k>20)&&(k<100))
    {
        op1(num/1000);
        printf(" thousand ");
        if(j>0&&j<10)
            op0(num%1000);
        if(j>10&&j<100)
            op1(num%1000);
        else
            op2(num%1000);
    }
//op2(num%1000);
}

void op5(long num)
{
    int k=num/100000;
    int j=num%100000;
    for(i=0; i<10; i++)
    {
        if(fig[i].dig==k)
        {
            printf("%s lakh ",fig[i].wrd);
            if(j>0&&j<20)
                op0(num%100000);
            if(j>20&&j<100)
                op1(num%100000);
            if(j>100&&j<1000)
                op2(num%100000);
            if(j>1000&&j<10000)
                op3(num%100000);
            if(j>10000&&j<100000)
                op4(num%100000);

        }
    }
}

void main()
{
    long num;
//    clrscr();
    printf("\nEnter the number: \n\t");
    scanf("%ld",&num);
    if(num<=20)
        op0(num);
    if((num>20)&&(num/100==0))
        op1(num);
    if(num==100)
        printf("hundred");
    if(num>100&&num<1000)
        op2(num);
    if(num==1000)
        printf("one thousand");
    if(num>1000&&num<10000)
        op3(num);
    if(num==10000)
        printf("ten thousand");
    if(num>10000&&num<100000)
        op4(num);
    if(num==100000)
        printf("one lakh");
    if(num>100000&&num<1000000)
        op5(num);
    if(num==1000000)
        printf("One crore");
    printf("\n\n");
    getchar();
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment