2013年10月10日 星期四

UVa 492 - Pig-Latin

Problem link

Keyword: ad hoc

Algorithm:

Um... I used cin.peek() method to check if next letter is an alphabet.
Do the specified action according to the problem statement and then you're home.

Code:
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
inline bool isvowel( char a ) { return a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'; }

int main()
{
    char ch;

    while( scanf( "%c", &ch ) != EOF )
    {
        if( !isalpha( ch ) )
            putchar( ch );
        else if( isvowel( tolower( ch ) ) )
        {
            putchar( ch );
            while( isalpha( cin.peek() ) )
                putchar( getchar() );
            printf( "ay" );
        }
        else
        {
            while( isalpha( cin.peek() ) )
                putchar( getchar() );
            printf( "%cay", ch );
        }
    }

    return 0;
}

沒有留言:

張貼留言