Thread: How do I change intergers in a string to their word equivalent (ie 1=one, 2=two)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    How do I change intergers in a string to their word equivalent (ie 1=one, 2=two)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        int n1;
        int i=0;
        char str1[100]; 
        char str2[100];
        
        printf("Please enter 1st number: ");
        scanf("%s", str1); 
        
        printf("\nPlease enter 2nd number: ");
        scanf("%s", str2); 
        
        printf("\nConcatenation of 1st & 2nd: %s\n", strcat(str1, str2));
        getchar(); 
        
        n1 = atoi( strcat(str1, str2) );
        printf("\nOnly Digits of the Concatenation: %d\n", n1);
        
        
        system("PAUSE");
        return 0;
    }
    This program so far asks for 1st string and 2nd string, then adds them together, then removes any letters, the next part I need to change all the digits its found to their word equivalent (ie 1=one, 2=two)

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    There's no API that does this conversion for you, so you'll have to have some kind of data structure that maps numbers to names. Obviously you don't want to do this for every single number, just the ones that have a single word that represents them, then combine the words based on whatever value you have.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    void foo(int value);
    
    int main(int argc, char *argv[])
    {
        char line[100];
        int i, n1, n2, num;
    
        
        char str1[100]; 
        char str2[100];
        
        printf("Please enter 1st number: ");
        scanf("%s", str1); 
        
        printf("\nPlease enter 2nd number: ");
        scanf("%s", str2); 
        
        printf("\nConcatenation of 1st & 2nd: %s\n", strcat(str1, str2));
        getchar(); 
        
        n1 = atoi( strcat(str1, str2) );
        printf("\nOnly Digits of the Concatenation: %d\n", n1);
        
        foo(n1);
        putchar('\n');
    
        
        
        system("PAUSE");
        return 0;
    }
    
    void foo(int value)
    {
       static const char *digit[] = 
       {
          "zero", "one", "two", "three", "four", 
          "five", "six", "seven", "eight", "nine",
       };
       if ( value > 0 )
       {
          foo(value / 10);
          printf("%s ", digit [ value % 10 ] );
       }
    }
    I almost have it, this doesnt covert fully if theres a word/letter in the n1 =/

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    void foo(int value);
    
    
    int main(int argc, char *argv[])
    {
        int n1, n2, i=0;
        char str1[100]; 
        char str2[100];
        
        printf("Please enter 1st number: ");
        scanf("%s", str1); 
        
        printf("\nPlease enter 2nd number: ");
        scanf("%s", str2); 
        
        strcat(str1, str2);
        printf("\nConcatenation of 1st & 2nd: %s\n", str1);
        
        n1 = atoi( str1 );
        printf("\nOnly Digits of the Concatenation: %d\n", n1);
        
        n2 = n1;
        foo(n2);
        
        
    
        system("PAUSE");
        return 0;
    }
    
    void foo(int value)
    {
       static const char *digit[] = 
       {
          "zero", "one", "two", "three", "four", 
          "five", "six", "seven", "eight", "nine",
       };
       if ( value > 0 )
       {
          foo(value / 10);
          printf("%s ", digit [ value % 10 ] );
       }
    }
    ^works if no letters are entered, but I want it to take out the letters and concat

    ie

    1st string: 123
    2nd string: 4dog56
    concat: 123456
    convert: one two three four five six

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    atoi stops the integer conversion when it encounters a character. You'll need to create a new string that only contains numbers and work with that.

    Actually, a better way to handle this is to just take each character one at a time, and if it's a digit print the name. You can convert a character representing a digit by subtracting '0' (i.e. the ascii value for the character 0). That way, you don't have to worry about running over INT_MAX.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM