ACM (UVa) : 10921


Find the Telephone

Data type:
Long Integer and String

Algorithm:

print the characters as it is, but if [A-Z] then print the numbers mentioned
In C/C++ it is usually considered that, gets () is a dangerous function. In this problem it is stated that, “The input is terminated by end of file (EOF).” One may face some problems to use scanf () bcoz it may not work properly when EOF entered. So, gets () would be used. [EOF >>> (^Z) >>> ctrl + z]

Advertisements

4 thoughts on “ACM (UVa) : 10921

  1. plz can u tell me what is the wrong in my code. here it is:

    #include

    main() {
    int i=0, j;
    char ch, store[50];
    while((ch=getchar())!=’\n’) {
    store[i] = ch;
    i++;
    }
    for(j=0; j<i; j++) {
    if((store[j]=='A') || (store[j]=='B') || (store[j]=='C')) {
    printf("2");
    }
    else if((store[j]=='D') || (store[j]=='E') || (store[j]=='F')) {
    printf("3");
    }
    else if((store[j]=='G') || (store[j]=='H') || (store[j]=='I')) {
    printf("4");
    }
    else if((store[j]=='J') || (store[j]=='K') || (store[j]=='L')) {
    printf("5");
    }
    else if((store[j]=='M') || (store[j]=='N') || (store[j]=='O')) {
    printf("6");
    }
    else if((store[j]=='P') || (store[j]=='Q') || (store[j]=='R') || (store[j]=='S')) {
    printf("7");
    }
    else if((store[j]=='T') || (store[j]=='U') || (store[j]=='V')) {
    printf("8");
    }
    else if((store[j]=='W') || (store[j]=='X') || (store[j]=='Y') || (store[j]=='Z')) {
    printf("9");
    }
    else {
    printf("%c", store[j]);
    }
    }
    printf("\n");
    return 0;
    }

  2. take the input using gets ()
    another thing, your code should take input until EOF

    coding format is like

    #include <stdio.h>
    
    int main ()
    {
        char store [50];
        
        while ( gets (store) ) {
            int length = strlen (store);
            
            for ( int i = 0; i < length; i++ ) {
                // place your if/else-if/else conditions here
            }
            
            printf ("\n");
        }
        
        return 0;
    }
    
    
  3. // See this code ,apply EOF condition

    #include
    int main()
    {
    char c;
    while(scanf(“%c”,&c)!=EOF)
    {
    if(c==’\n’)
    {
    printf(“\n”);
    continue;
    }

    switch(c)
    {
    case ‘A’:
    case ‘B’:
    case ‘C’:
    { printf(“2”); continue; }
    case ‘D’:
    case ‘E’:
    case ‘F’:
    { printf(“3”); continue; }
    case ‘G’:
    case ‘H’:
    case ‘I’:
    { printf(“4”); continue; }
    case ‘J’:
    case ‘K’:
    case ‘L’:
    { printf(“5”); continue; }
    case ‘M’:
    case ‘N’:
    case ‘O’:
    { printf(“6”); continue; }
    case ‘P’:
    case ‘Q’:
    case ‘R’:
    case ‘S’:
    { printf(“7”); continue; }
    case ‘T’:
    case ‘U’:
    case ‘V’:
    { printf(“8”); continue; }
    case ‘W’:
    case ‘X’:
    case ‘Y’:
    case ‘Z’:
    { printf(“9”); continue; }

    case ‘-‘:
    case ‘1’:
    case ‘0’:
    { printf(“%c”,c); }
    }
    }
    return 0;
    }

  4. I am always fan of your coding philosophy and I am helped many times from your code when I got stuck in a problem.Many of my friends in KUET also admire of your code.Keep it up 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s