How to print the text string correctly?

1
  

Create a program in for the following statement:

     
    

The program must request a whole number that includes up to 3 digits (units, tens and / or hundreds), and then present that number written in words. For example: if we send 57 the result must be FIFTY AND SEVEN. Define at least 3 functions where each represents the units, tens and hundreds, in each function there must be an arrangement with the words. You should also use a function that returns the final words that correspond to the number entered and present it in the main.

  

So this is my code and at the time of printing it does not work out as it should.

I know I'm still missing several things

#include<stdio.h> 
#include<conio.h>
#include<string.h>

char *unidad(int f1);
char *decena(int f2);
char *centena(int f3);

main(){

  int numero,u, d, c;
  char j[6];
  char k[7];
  char l[10];


printf("\n\nBIENVENIDO INGRESE UN NUMERO (1 AL 999)\n\n");
  printf("INTRODUZCA EL NUMERO: ");
    scanf("%d",&numero);

  u=numero%10;
    numero=numero/10;
    d=numero%10;
    numero=numero/10;
   c=numero%10;

   if(c!=0 && d!= 0){
        printf("LA CENTENA ES: %d\n\n",c);
         printf("LA DECENA ES: %d\n\n", d);
        printf("LA UNIDAD ES: %d\n", u);
    }

 else{
 if(d!= 0){
  printf("LA DECENA ES: %d\n\n", d);
  printf("LA UNIDAD ES: %d\n\n", u);
 }


    else{
        printf("LA UNIDAD ES: %d\n\n", u);
    }
 }



strcpy(j, unidad(u));
  strcpy(k, decena(d));
  strcpy(l, centena(c));

   printf(" el numero en palabras es: %s %s %s",l, k, j);
   getch();




}//MAIN



char *unidad(int f1){
 char arreglo1[9][6]={"UNO","DOS","TRES", "CUATRO","CINCO","SEIS","SIETE","OCHO","NUEVE"};
  return(arreglo1[f1-1]);
 }

char *decena(int f2){
    char arreglo2[7][8]={"DIEZ","DIECI","ONCE","DOCE", "TRECE", "CATORCE","QUINCE"};

  char arreglo3[16][30]={"VEINTE","VEINTI ","TREINTA", "TREINTA Y ", "CUARENTA","CUARENTA Y ","CINCUENTA","CINCUENTA Y","SESENTA","SESENTA Y ","SETENTA","SETENTA Y","OCHENTA","OCHENTA Y","NOVENTA","NOVENTA Y"};

  return(arreglo2[f2-1]);





}
char *centena(int f3){
    char arreglo4[10][20]={"CIEN","CIENTO","DOSCIENTOS","TRECIENTOS","CUATROCIENTOS","QUINIENTOS","SEISCIENTO","SETECIENTOS","OCHOCIENTOS","NOVECIENTOS",};
   return(arreglo4[f3-1]);

 }
    
asked by Guillermo Navarro 05.07.2017 в 16:51
source

2 answers

0

First, they tell you that you have to convert a number into its written representation, that is:

  • 1 - > one
  • 102 - > one hundred two

So the output of the program should be something such that:

printf("%s%s%s",cadena_centenas,cadena_decenas,cadena_unidades);

To achieve this sequence we have to pay attention to the characteristics of the tens and the hundreds:

  • numbers from 11 to 15 have a separate nomenclature.
  • the whole dozens are written different to the tens with units:

    20 -> veinte
    21 -> veintiuno
    30 -> treinta
    31 -> treinta y uno
    
  • The hundreds are written differently if they do not have units or tens:

    100 -> cien
    101 -> ciento uno
    

With this in mind the function of the units is trivial:

char *unidad(int unidades){
 char *arreglo[]={"","UNO","DOS","TRES", "CUATRO","CINCO","SEIS","SIETE","OCHO","NUEVE"};
 return arreglo[unidades];
}

The explanation is simple: each position corresponds to its direct translation ... position 0 is empty because zero has no translation but is a digit to be taken into account.

The tens are a little complicated because, as we have seen, they can depend on the units:

char *decena(int decenas, int unidades){
  char *arreglo1[]={"","DIEZ","VEINTE","TREINTA","CUARENTA", "CINCUENTA", "SESENTA", "SETENTA", "OCHENTA", "NOVENTA"};
  char *arreglo2[]={"","DIECI","VEINTI","TREINTA Y ","CUARENTA Y ", "CINCUENTA Y ", "SESENTA Y ", "SETENTA Y ", "OCHENTA Y ", "NOVENTA Y "};

  if( unidades == 0 )
    return arreglo1[decenas];
  else
    return arreglo2[decenas];
}

The explanation remains simple: If the units are 0 then an element of the first array is returned; in the rest of the cases an element of the second arrangement is returned. Again we reserve a space in the array in case the units are worth 0 (empty string)

And with the hundreds it happens exactly the same as with the units:

char *centena(int centenas, int decenas, int unidades){
  char *arreglo1[]={"","CIEN","DOSCIENTOS","TRESCIENTOS","CUATROCIENTOS","QUINIENTOS","SEISCIENTOS","SETECIENTOS","OCHOCIENTOS","NOVECIENTOS"};
  char *arreglo2[]={"","CIENTO ","DOSCIENTOS ","TRESCIENTOS ","CUATROCIENTOS ","QUINIENTOS ","SEISCIENTOS ","SETECIENTOS ","OCHOCIENTOS ","NOVECIENTOS "};

  if( decenas == 0 && unidades == 0)
    return arreglo1[centenas];
  else
    return arreglo2[centenas];
}

And it's already ... no, wait. Missing numbers that go for free (range 11-15). These numbers have as a feature that group unit and decade so we dedicate a function of their own:

char *especiales(int decenas, int unidades)
{
  char *arreglo[6] = {"","ONCE","DOCE","TRECE","CATORCE","QUINCE"};

  int valor = decenas*10+unidades;

  if( valor >= 11 && valor <= 15 )
    return arreglo[valor-10];
  else
    return arreglo[0];
}

It's easy to explain ... if the number is in the range 11-15, we return the corresponding direct translation and otherwise return an empty string.

How is all this grouped? With a main the sea of simple:

main(){

  printf("\n\nBIENVENIDO INGRESE UN NUMERO (1 AL 999)\n\n");
  while(true)
  {
    printf("INTRODUZCA EL NUMERO: ");
    scanf("%d",&numero);

    int u=numero%10;
    numero=numero/10;
    int d=numero%10;
    numero=numero/10;
    int c=numero%10;

    char* cadena_centena  = centena(c,d,u);
    char* cadena_decena   = decena(d,u);
    char* cadena_unidad   = unidad(u);
    char* cadena_especial = especiales(d,u);

    if( *cadena_especial )
    {
      printf("%s%s\n",cadena_centena,cadena_especial);
    }
    else
    {
      printf("%s%s%s\n",cadena_centena,cadena_decena,cadena_unidad);
    }
  }
}

Since it is not my intention to give it all done (if you want to copy the exercise as you think you may be asked why you did it this way and not another), the task of understanding this part of the code runs from your account.

    
answered by 05.07.2017 / 17:36
source
1

This answer is not to solve the exercise, however I explain why this happens to you. The problem is in decena :

char *decena(int f2){
  char arreglo2[7][8]={"DIEZ","DIECI","ONCE","DOCE", "TRECE", "CATORCE","QUINCE"};

  char arreglo3[16][30]={"VEINTE","VEINTI ","TREINTA", "TREINTA Y ", "CUARENTA","CUARENTA Y ","CINCUENTA","CINCUENTA Y","SESENTA","SESENTA Y ","SETENTA","SETENTA Y","OCHENTA","OCHENTA Y","NOVENTA","NOVENTA Y"};

  return(arreglo2[f2-1]);
}

As you can see in the return, you always return arreglo2[f2-1] . This is the arrangement in case the ten is 1, it would adopt that form depending on the unit. You also need to use arreglo3 which is the one that gives you this value. For example if you call decena(2) then arreglo2[1] = "DIECI" , when the expected result would be "TWENTY" or "VEINTI" depending on the units.
You can help with these links so you can see other examples of this exercise
link
link

    
answered by 05.07.2017 в 17:28