Apobia
Apobia

Reputation: 35

Converting numbers to words in array

I need to convert numbers to words in array, that we print from keyboard(array include other words too)

As an example . Input text: I have 2 apples. Output : I have two apples/How to do it ?Or how it write by using itoa?

#include <stdio.h>
#include <stdlib.h>
#define MAX 170
int main(void) {
    const char* fkr[10] = { "zero" ,"one", "two", "three", "four", "five", "six","seven","eight","nine" };
    char* fk, ar;
    char afk[MAX] = {};
    gets_s(afk);
    return 0;
}

Upvotes: 1

Views: 195

Answers (1)

chux
chux

Reputation: 154169

OP is on the right track.

Look at each char of input for digits and replace with text when needed. When a digit is found, use it - '0' to index the number array.

#include <stdio.h>
#include <stdlib.h>
#define MAX 170

int main(void) {
  const char* fkr[10] = { "zero" ,"one", "two", "three", "four",
      "five", "six","seven","eight","nine" };
  char* fk;
  char afk[MAX];
  if (fgets(afk, sizeof afk, stdin)) {
    char *fk = afk;
    while (*fk) {
      if (*fk >= '0' && *fk <= '9') {
        fputs(fkr[*fk - '0'], stdout);
      } else {
        putchar(*fk);
      }
      fk++;
    }
  } 
  return 0;
}

Consider additional code to handle back-co-back digits. oneone looks strange. Perhaps "one one" or "eleven".

Upvotes: 2

Related Questions