/*   
     Author: Jesse Demarco
     Date:   Feb. 22, 2007

     Description:  This program prompts the user to enter a number. The program 
                   accepts the largest unsigned integer the system will allow.
                   The program than outputs the "look and say" value of the 
                   number.
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h> 
#include <string.h>
#include <ctype.h>


int main(void)
{
   unsigned long int i;  /* Inputed Unsigned Long Integer. */
   int temp; /* Temp variable*/
   
   int look_say[20]; /* Array for the final look and say value */
   int t[20];        /* Array for testing the inputed Number. */  
   int count = 0, c = 0, checked = 0;     /*count variables*/

   char s[15]; /*Character string to find the length of the inputed number*/
   int length_num =0, length_numcpy =0; /* Two variable for the character length of input*/
  
   printf("Enter an integer \n");
   scanf("%lu", &i);
   
   printf("The number accepted is: %lu\n", i); /* Echo the number entered*/
   sprintf(s, "%lu", i);         /*Store the input number as a character string*/ 
   length_num = strlen(s); /* Find the character length of the number*/
   length_numcpy = length_num; /*set another variable to the char length of the number*/
   
   /* Each increment gets the value of the digit in the tenths place */
   for (count = 0; count < length_num; ){
   temp = i % 10;        /* Get the current number in the tenths place */
   t[(int)count] = temp; /* Put that number into the array*/ 
   i -= temp; /* Chop off the last digit */
   i /= 10;   /* Chop off the last zero */   
   ++count;
   } /*Loop is finished, we now have the inputed number stored as the array t */
    
 /*Outer loop runs until every digit has been checked*/  
 while (checked < length_numcpy){
   count = 1; /*reset count, there is at least one of any number found*/

   /*Inner loop counts the number of matching consecutive digits*/
   while(((t[length_num-1]) == (t[length_num-2]))&& (length_num > 1)){ 
                     count +=1;
                     length_num--;             
                    } 
   /* Inner loop has finished, decrement the index once more unless its already zero*/   
   if (length_num > 0)               
   length_num--;               
                    
    /* Add to the array two indices at a time */                       
   look_say[c] = count;       /*  Insert the consecutive number count */
   look_say[c+1] = t[length_num]; /*Insert the last number to match */ 
   c +=2; /* Increment the index of the look and say value array by two*/
   
   checked+= count; /*Keep count of how many digits the inner loop has checked*/
} /*Outer loop has finished, we have the look and say value. */

   count =0;
   printf("The 'look and say' value is: "); 
   
   /*Print the look and say array */
    for (c = c; c > 0; ){
   printf("%d", look_say[count]);
   --c;
   count++;
   }
   printf("\n");

}