Ad

Wednesday, September 4, 2013

Uva 11530 - SMS Typing

SMS Typing in Uva is a string related problem. Before writing the code you should know about string. Let's see What is string ?

string : string in C Programming Language is actually a one-dimensional array of characters which is terminated by a null character '\0'.

The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello".



char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as follows:
 
char greeting[] = "Hello";
 
 
Now getting back on SMS Typing. Click Here to Read the Problem Description
 
 
 
Source Code : 
 
#include<stdio.h>
#include<string.h>

main()
{
    char str[105];
    int press, test, i, index, len;

    scanf("%d", &test);
    getchar();
    for(i=1;i<=test;i++)
    {
        gets(str);
        press=0;
        len=strlen(str);
        for(index=0;index<len;index++)
        {
            if(str[index]==' '||str[index]=='a'||str[index] =='d'||str[index]=='g'||str[index] == 'j' || str[index] == 'm' || str[index] == 'p' || str[index] == 't' || str[index] == 'w')
            {
                press++;
            }
            else if(str[index] == 'b' || str[index] == 'e' || str[index] == 'h' || str[index] == 'k' || str[index] == 'n' || str[index] == 'q' || str[index] == 'u' || str[index] == 'x')
            {
                press+=2;
            }
            else if(str[index] == 'c' || str[index] == 'f' || str[index] == 'i' || str[index] == 'l' || str[index] == 'o' || str[index] == 'r' || str[index] == 'u' || str[index] == 'y' || str[index] == 'v')
            {
                press+=3;
            }
            else if(str[index] == 's' || str[index] == 'z')
            {
                press+=4;
            }
        }
        printf("Case #%d: %d\n", i, press);
    }

return 0;
}
 

0 comments:

Post a Comment