The code below let's you enter a number and convert it to its words form. For instance an input of 100 will result to an output of one hundred. The next step I need to do is to count the number of letters on the word form of the number. How can I store the value of convertToWords function onto a variable so it can be an input variable for the next line of codes which will count the number of letters.
This is the code for converting words to numbers
#include<string.h>
#include<stdio.h>
#define BUF_SIZE 20
// strings at index 0 is not used, it is to make array
// indexing simple
char* one[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};
// strings at index 0 and 1 are not used, they is to
// make array indexing simple
char* ten[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety "
};
char * numToWords(int n, char* s, char *str, int len)
{
memset(str,0,len);
// if n is more than 19, divide it
if (n > 19)
{
strcat(str,ten[n / 10]);
strcat(str,one[n % 10]);
}
else
{
strcat(str,one[n]);
}
// if n is non-zero
if (n)
{
strcat(str,s);
}
return str;
}
// Function to print a given number in words
char* convertToWords(long n, char *out)
{
char str[BUF_SIZE] = {0};
// handles digits at ten millions and hundred
// millions places (if any)
strcat(out, numToWords((n / 10000000), "million ",str,BUF_SIZE));
// handles digits at hundred thousands and one
// millions places (if any)
strcat(out, numToWords(((n / 100000) % 100), "hundred thousand ",str,BUF_SIZE));
// handles digits at thousands and tens thousands
// places (if any)
strcat(out, numToWords(((n / 1000) % 100), "thousand ",str,BUF_SIZE));
// handles digit at hundreds places (if any)
strcat(out, numToWords(((n / 100) % 10), "hundred ",str,BUF_SIZE));
//Increase code readability
// handles digits at ones and tens places (if any)
strcat(out, numToWords((n % 100), "",str,BUF_SIZE));
return out;
}
int main()
{
//Get input number from user
long num;
char str[60] = {0};
char s[10000];
printf("Enter any number: ");
scanf("%ld", &num);
printf( "%s",convertToWords(num,str));
return 0;
}
This will be the next line of code which will count the number of letters on the word form of a number.I am thinking of storing the value of convertToWords function to a variable then that variable will be an input for the following code. However equating convertToWords(num,str) to an array does not work. If there are other way two combine these two codes feel free to suggest. For example input of 100, output must be 10 since there are ten letter in one hundred. Thank you
#include<stdio.h>
int main()
{
//variable declaration
char word[100];
int a;
int counting=0;
int wordcountig=0;
//Input word
printf("Enter word to count the number of characters\n");
//to read word from user
gets(word);
//to read characters
for(a=0; word[a]!='\0'; a )
{
counting ;
}
for(a=0; a<counting; a )
{
if(word[a]==32)
wordcountig ;
}
//Display number of characters in the word
wordcountig = counting-wordcountig;
printf("\n\n The number of characters in entered word are : %d", wordcountig);
return 0;
}
CodePudding user response:
You have the resulting string in str
in main
. So, just use a loop to count the letters:
int count = 0;
for (int i = 0; str[i]; i)
if (isalpha(str[i]))
count;
Here is the full program, including some fixes for buffer overflow (I increased the sizes) and for larger numbers:
#include<string.h>
#include<stdio.h>
#include <ctype.h>
#define BUF_SIZE 200
// strings at index 0 is not used, it is to make array
// indexing simple
char* one[] = { "", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen "
};
// strings at index 0 and 1 are not used, they is to
// make array indexing simple
char* ten[] = { "", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety "
};
char * numToWords(unsigned int n, char* s, char *str, int len)
{
memset(str,0,len);
// if n is more than 19, divide it
if (n > 19)
{
strcat(str,ten[n / 10]);
strcat(str,one[n % 10]);
}
else
{
strcat(str,one[n]);
}
// if n is non-zero
if (n)
{
strcat(str,s);
}
return str;
}
// Function to print a given number in words
char* convertToWords(long n, char *out)
{
char str[BUF_SIZE] = {0};
// handles digits at ten millions and hundred
// millions places (if any)
strcat(out, numToWords((n / 1000000), "million ",str,BUF_SIZE));
// handles digits at hundred thousands and one
// millions places (if any)
strcat(out, numToWords(((n / 100000) % 10), "hundred ",str,BUF_SIZE));
// handles digits at thousands and tens thousands
// places (if any)
strcat(out, numToWords(((n / 1000) % 100), "thousand ",str,BUF_SIZE));
// handles digit at hundreds places (if any)
strcat(out, numToWords(((n / 100) % 10), "hundred ",str,BUF_SIZE));
//Increase code readability
// handles digits at ones and tens places (if any)
strcat(out, numToWords((n % 100), "",str,BUF_SIZE));
return out;
}
int main()
{
//Get input number from user
long num;
char str[600] = {0};
scanf("%ld", &num);
printf("%s\n",convertToWords(num,str));
int count = 0;
for (int i = 0; str[i]; i) if (isalpha(str[i])) count;
printf("It has %d characters\n", count);
return 0;
}
https://godbolt.org/z/noh5bG36G
If you do not need the text output, you can change printf( "%s",convertToWords(num,str));
to convertToWords(num,str);
.