Hello, I am fairly new to programming and my professor brought up the following problem today:
User enters sentence "The Smiths have two daughters, three sons, two cats and one dog." (The numbers may change depending on what the user chooses to enter. He told us the range would be from zero to nine.) and we have to convert the written numbers within the sentence into actual decimal numbers and print out the new sentence. Ex. The Smiths have 2 daughters, 3 sons...etc.
I have written the following bit of code which reads the string and finds all the "written numbers" but I am not sure how to proceed from there. I am stuck on how to print out the new sentence with the converted numbers as my professor mentioned something about creating the new string using dynamic memory allocation. I am aware that my code is probably incorrect so any suggestions and pointers will be appreciated. Thank you.
Code:
#include <stdio.h>#include <string.h>
int main () {
char A[100];
int length = 0;
int i;
printf ("Please enter a sentence you want to convert:");
gets(A);
while (A[length]!='\0') length ++;
printf ("%d\n", length);
for (i=0; i<length; i++){
if (A[i]=='z'&& A[i+1]=='e' && A[i+2]=='r' && A[i+3]=='o')
A[i]=0;
else if (A[i]=='o'&& A[i+1]=='n' && A[i+2]=='e')
A[i]=1;
else if (A[i]=='t'&& A[i+1]=='w' && A[i+2]=='o')
A[i]=2;
else if (A[i]=='t'&& A[i+1]=='h' && A[i+2]=='r' && A[i+3]=='e' && A[i+4]=='e')
A[i]=3;
else if (A[i]=='f'&& A[i+1]=='o' && A[i+2]=='u'&& A[i+3]=='r')
A[i]=4;
else if (A[i]=='f'&& A[i+1]=='i' && A[i+2]=='v'&& A[i+3]=='e')
A[i]=5;
else if (A[i]=='s'&& A[i+1]=='i' && A[i+2]=='x')
A[i]=6;
else if (A[i]=='s'&& A[i+1]=='e' && A[i+2]=='v'&& A[i+3]=='e'&& A[i+4]=='n')
A[i]=7;
else if (A[i]=='e'&& A[i+1]=='i' && A[i+2]=='g'&& A[i+3]=='h'&& A[i+4]=='t')
A[i]=8;
else if (A[i]=='n'&& A[i+1]=='i' && A[i+2]=='n'&& A[i+3]=='e')
A[i]=9;
else
continue;
}
return 0;
}