Finding "written numbers" in a string and converting them to decimal numbers

This is a discussion on Finding "written numbers" in a string and converting them to decimal numbers within the C Programming forums, part of the General Programming Boards category; Hello, I am fairly new to programming and my professor brought up the following problem today: User enters sentence "The ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    Finding "written numbers" in a string and converting them to decimal numbers

    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;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You should use fgets instead of gets. Maybe I should say that you must use fgets.

    You have to break down your problem. Read the data and then print them. You think you are in the second, but **maybe** you are not. You increase the counter of your loop (variable i) by one, even though you may have found nine, but you do not increase by 3 (because the for loop will increase it by one), but I think that this ok for now. Let's go at the printing phase.

    Print your array and make sure that it holds the correct numbers.

    Then, you could either overwrite array A, but you should move some letters, because "has one son" has to be "has 1 son", so you can not just replace the 'n' and 'e' with whitespaces, or you could starting copying the A to another array, named answer.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    Thank you for the reply . Can you please explain the part where you said "but you do not increase by 3"? I am afraid I didn't quite get that. Many thanks!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I was expecting a different algorithm. Where the written numbers were in a 2D char array, and strstr() was used to locate them. Then the index of the written numbers would replace the letters of that number, in a duplicate array, and then proceed like that, until no more written numbers were found.

    What is the index number?
    Code:
    const char numbers[10][6]= {{"zero"},{"one"},{"two"}, etc.... };
    So "zero" will be replaced by it's index - 0. One by 1, etc.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    Oh I never thought about that as we were only taught the strlen function within the string library and are usually encouraged to write our own functions instead of using the string functions provided. I'll look into that. Thank you!

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    @Adak. I was expecting an algorithm Lamio posted, since he is a starter. And he has done very good so far.

    @Lamio
    Firstly, welcome to the forum.

    Well, you can found the o from one ~for example~ in position A[4], then you increase i by one, thus you check the position A[5], which is not a must. You know already that A[5] and A[6] are not places that you have to look into to. But this does not harm the correctness of your algorithm, at a first glance.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Lamio View Post
    Oh I never thought about that as we were only taught the strlen function within the string library and are usually encouraged to write our own functions instead of using the string functions provided. I'll look into that. Thank you!
    Yes you are, in order to practice.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by Lamio View Post
    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.
    The first thing is to write a function which takes a "word" and finds in in English input. This is not the same as finding a substring. "tenant" is not the word "ten", nor is "often" or "intent". "Ten houses" is, "You want ten?" is, "milk,ten Player's cigarettes" is a typographical error, but you probably want to accept it. "TEN" arguably isn't, it's probably an acronym for Tenant's Education Network or something, but this is a slightly ambiguous area in the spec.

    The second thing it to write routine to replace a substring (pass index and length) with another, using malloc to return the new string.
    Then put your two functions together in a controlling "replace words with digits" function.
    You can now easily extend it to cope with the teens. Going in reverse is also possible with a few modifications, but a lot harder once you get multi-digit numbers.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a "large" number of prime numbers
    By javaeyes in forum General Discussions
    Replies: 7
    Last Post: 09-20-2012, 03:54 PM
  2. Replies: 12
    Last Post: 07-05-2012, 08:32 AM
  3. Replies: 31
    Last Post: 08-16-2011, 08:32 AM
  4. Replies: 31
    Last Post: 11-25-2009, 12:10 PM
  5. converting "String___gc" simply to "string"
    By mitilkhatoon in forum C++ Programming
    Replies: 12
    Last Post: 09-29-2006, 04:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21