Convert an integer to words : Int Convert « Data Type « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » Data Type » Int ConvertScreenshots 
Convert an integer to words

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/
/*  */
#include <stdio.h>
#include <string.h>

void main()
{
  char *unit_words[] {"zero""one","two","three","four","five","six","seven","eight","nine"};
  char *teen_words[] {"ten""eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
  char *ten_words[] {"error""error","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
  char hundred[] " hundred";
  char and[] " and ";
  char value_str[50"";
  int value = 0;                       /* Integer to be converted        */
  int digits[] {0,0,0};            /* Stores digits of value entered */
  int i = 0;

  printf("Enter an integer less than 1000: ");
  scanf("%d",&value);
  if(value>=1000)
    value =999;
  else if(value<1)
    value = 1;

  while(value>0)
  {
    digits[i++= value%10;
    value /= 10;
  }

  if(digits[20)
  {
    strcat(strcat(value_str,unit_words[digits[2]]), hundred);
    if(digits[1>|| digits[00)
      strcat(value_str, and);
  }
  if(digits[10)
  {
    if(digits[1== 1)
      strcat(value_str,teen_words[digits[0]]);
    else
    {
      strcat(value_str,ten_words[digits[1]]);
      if(digits[00)
        strcat(strcat(value_str, " "), unit_words[digits[0]]);
    }
  }
  else
    if(digits[00)
      strcat(value_str, unit_words[digits[0]]);
  printf("\n%s\n", value_str);
}

 

           
       
Related examples in the same category
1.A function to return a string representation of an integer with a given widthA function to return a string representation of an integer with a given width
2.A function to return a string representation of an integerA function to return a string representation of an integer
3.Convert integer to string: how to use itoa
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.