T : Santagram Checker

20Dec12

I recently participated in the Advent Programming Contest organised by the IEEE Student Branch Klagenfurt (with support from Alpen-Adria-Universität Klagenfurt and Universität Passau), achieving a rank of 11th (out of 118 participants); as submissions of solutions to that contest are now closed, I’m posting here the solutions I submitted to that contest.

Problem (T; easy difficulty):

Santagram Checker

Santa is a big fan of anagrams. An anagram is a word or phrase that exactly reproduces the letters of a given phrase or word in another order. Santa even invented his own version of anagrams, the so called Santagrams, which are build by rearranging the letters of “SANTA CLAUS”.

Problem

Implement a program that checks if a given input (all characters will be uppercase) is a Santagram or not. A Santagram may contain extra spaces or no spaces. The order of the letters must change, so the original “SANTA CLAUS” is not a Santagram. Depending on the check result, your program should output “A Santagram!” or “Not a Santagram!” (without the quotes, with a newline) and terminate.

Example

Input

SATAN LUCAS

Output

A Santagram!

Input

A CLASSY AUNT

Output

Not a Santagram!

Solution (T.c):

#include <stdio.h>
#include <string.h>

typedef enum
{
  false = 0,
  true
}
bool;

static const char * const k_ORIGINAL_WORD = "SANTA CLAUS";

int main( void )
{
  size_t i = 0;
  int l[ 26 ];
  for( ; i < 26; i ++ )
    l[ i ] = 0;
  char c;
  for( i = 0; i < strlen( k_ORIGINAL_WORD ); i ++ )
  {
    c = k_ORIGINAL_WORD[ i ];
    if(( c >= 'A' ) && ( c <= 'Z' ))
      l[ c - 'A' ] ++;
  }
  i = 0;
  bool isSantagram = true, isOriginalWord = true;
  do
  {
    scanf( "%c", &c );
    if( c != '\n' )
      if(( c >= 'A' ) && ( c <= 'Z' ))
      {
        l[ c - 'A' ] --;
        if( l[ c - 'A' ] < 0 )
          isSantagram = false;
        if( isOriginalWord )
        {
          if( i >= strlen( k_ORIGINAL_WORD ))
            isOriginalWord = false;
          else
            if( c != k_ORIGINAL_WORD[ i ] )
              isOriginalWord = false;
        }
      }
    i ++;
  }
  while(( c != '\n' ) && isSantagram );
  if( isOriginalWord )
    isSantagram = false;
  else
    for( i = 0; (( i < 26 ) && isSantagram ); i ++ )
      if( l[ i ] > 0 )
        isSantagram = false;
  if( !isSantagram )
    printf( "Not a" );
  else
    printf( "A" );
  printf( " Santagram!\n" );
  return 0;
}

Testing:

gcc -std=c99 -Wall -lm T.c
mv a.out T
./T
SANTA CLAUS
Not a Santagram!
./T
SATAN LUCAS
A Santagram!
./T
A CLASSY AUNT
Not a Santagram!
Advertisements


No Responses Yet to “T : Santagram Checker”

  1. Leave a Comment

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


%d bloggers like this: