Thread: need help with a program

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    6

    need help with a program

    hello,
    I need to write a program which will ask the user to enter a sentence and then the program will be able to break down words into letters and replace the letters with Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu (a= Alpha b=Bravo etc.) and the numbers with * (1= * 2=** 3=*** etc.)

    Let me give you an example: lets say that I run that program and the program asks my to write down my sentence and I want to break down the sentence “Hello 2 you “ (input) i want my program to return “Hotel Echo Lima Lima Oscar ** Yankee Oscar Uniform!.” (output)

    Thank you in advance!!!!


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,621
    So what have you managed to do so far?

    Can you read "Hello 2 you " into a char array, using say fgets?

    Can you iterate over the array and print each char in turn?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    6
    I manage to write:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    char * alphaTable[] = {
    "Alpha",
    "Bravo",
    "Charlie",
    "Delta",
    "Echo",
    "Foxtrot",
    "Golf",
    "Hotel",
    "India",
    "Juliet",
    "Kilo",
    "Lima",
    "Mike",
    "November",
    "Oscar",
    "Papa",
    "Quebec",
    "Romeo",
    "Sierra",
    "Tango",
    "Uniform",
    "Victor",
    "Whiskey",
    "Xray",
    "Yankee",
    "Zulu",
        NULL
    };
    
    
    void strtoupper(char *s)
    {
        while (*s)
        {
            if ((*s >= 'a') && (*s <= 'z'))
                *s -= ('a'-'A');
            s++;
        }
    }
    
    
    char * findMatch(char *table[], char c)
    {
        int i;
        for(i=0; table[i] != NULL; i++)
        {
    if ( table[i][0] == c) // if first character matches
            {
                return table[i];
            }
        }
    
    returnNULL;
    }
    
    
    int main()
    {
        int i;
        char sentace;
        printf("Write down your sentace \n");
        scanf("%c", &sentace);
    
        char input [] = "Hello 2 you!.";
    
    strtoupper(input);   // convert all letters to caps
    
        for (i=0;input[i]!=0;i++)
        {
    // take characters from the input string
            char c = input[i];
    
    // and find matching first letter in the alphaTable
            char * result = findMatch(alphaTable,c);
    
            if(result)
            {
                printf(" %s",result);
            }
    else// NOT A LETTER
            {
                if (isdigit(c)) // number
                {
    int num = c-'0'; // a short cut: get a number
                    int j;
    
                    printf(" ");
    
                    for(j=0;j<num;j++) // nothing printed for 0!
                    {
                        printf("%s","*");     
                    }
                }
    else// everything else is printed as is
                {
                    printf("%c",c);                   
                }    
            }
        }
    
        return 0;
    }

    but it works only for "Hello 2 you" and I want my program to work for any sentence not just "hello 2 you "


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,621
    Well all you need is to make your input array a decent size, then call fgets(), as suggested earlier.

    It's a remarkable improvement over your first post; you've managed to do all the hard work.

    Still, your strtoupper() function would benefit from using islower() and toupper() from ctype.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  5. Replies: 1
    Last Post: 03-03-2009, 04:47 PM

Tags for this Thread