Skip to content

Instantly share code, notes, and snippets.

@tinwritescode
Created April 23, 2020 15:57
Show Gist options
  • Save tinwritescode/233f2b7ad861ac093b6ba14d7e53d62c to your computer and use it in GitHub Desktop.
Save tinwritescode/233f2b7ad861ac093b6ba14d7e53d62c to your computer and use it in GitHub Desktop.
char* humanlizeString(const char *str) {
char *result = new char[strlen(str) * 10];
const char *name[] = {
"nghin", "trieu", "ti"
};
const char *number[] = {
"khong", "mot", "hai", "ba", "bon",
"nam", "sau", "bay", "tam", "chin"
};
const char *name2[] = {
"", "muoi ", "tram "
};
int n = strlen(str);
int readnum;
int pos = 0;
int prefix;
char *tmp = new char[100];
while(n != 0) {
readnum = n % 3 == 0 ? 3 : n % 3;
for(int i = pos, j = pos + readnum; i < j; i++) {
tmp[0] = '\0';
if(j - i == 2 && str[i] - '0' == 0) {
strcat(result, "le ");
continue;
}
else {
strcat(tmp, number[ str[i] - '0' ]);
}
strcat(tmp, " ");
strcat(result, tmp);
if(j - i != 0)
strcat(result, name2[j - i - 1]);
}
prefix = n % 3 == 0 ? (n - 1) / 3 : n / 3;
while(prefix > 3) {
prefix = prefix - 3;
}
if(prefix - 1 >= 0) {
strcat(result, name[prefix - 1]);
strcat(result, " ");
}
pos = pos + readnum;
n -= readnum;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment