/*
    statistic.c

    cipher statistic generator
    Copyright (C) 1997,1998 Robert Muth <muth@cs.arizona.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2 of June 1991.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program in form of the file COPYING; 
    if not, write to the 

    Free Software Foundation, Inc. <http://www.fsf.org>
    59 Temple Place, Suite 330, 
    Boston, MA 02111-1307  USA
*/



/*
 *               read stdin and generate monogram, bigram,
 *               trigram statistic on stdout.
 *               Those statistics are needed for the
 *               substitution and vigenere cipher breaker
 */

#include <stdio.h>
#include <ctype.h>

int stat1[32];
int stat2[32][32];
int stat3[32][32][32];
                 
int char2num(int c)
{
    if( isalpha(c) )
        return tolower(c) - 'a' + 1;
    else
        return 0;
}

int num2char(int c)
{
    if( c == 0)
        return ' ';
    else
        return c - 1 + 'a';
}

int main()
{
    int i1,i2,i3;

    i2 = char2num( getchar() );
    stat1[i2]++;
    i3 = char2num( getchar() );
    stat1[i3]++;
    stat2[i2][i3]++;
    
    for(;;)
    {
        int c = getchar();

        if( c == EOF )
            break;

        if( c == '\n' )
            continue;

        if( !isalpha(c) )
            continue;
           
        i1 = i2;
        i2 = i3;
        i3 = char2num( c );

        stat1[i3]++;
        stat2[i2][i3]++;
        stat3[i1][i2][i3]++;

    }

    for(i1=0; i1<27; i1++)
    {
        printf("%2d %d\n",i1,stat1[i1]);
    }

    for(i1=0; i1<27; i1++)
        for(i2=0; i2<27; i2++)
        {
            printf("%2d %2d %d\n",i1,i2,stat2[i1][i2]);
        }


    for(i1=0; i1<27; i1++)
        for(i2=0; i2<27; i2++)
            for(i3=0; i3<27; i3++)
            {
                printf("%2d %2d %2d %d\n",i1,i2,i3,stat3[i1][i2][i3]);
            }

    return 0;
}
/* eof */







