/*  Look-And-Say Sequence

	The idea for this problem comes from Wikipedia: 
	http://en.wikipedia.org/wiki/Look-and-say_sequence

	"The look-and-say sequence is the sequence of integers beginning as follows:	
	 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ...
	
	 To generate a member of the sequence from the previous member, read off 
	 the digits of the previous member, counting the number of digits in groups 
	 of the same digit. For example:
	
	 1 is read off as "one 1" or 11.
	 11 is read off as "two 1s" or 21.
	 21 is read off as "one 2, then one 1" or 1211.
	 1211 is read off as "one 1, then one 2, then two 1s" or 111221.
	 111221 is read off as "three 1s, then two 2s, then one 1" or 312211."


You must write a complete C program that reads in an element of the sequence 
and outputs the element immediately after it. For example, entering 312211 
should produce an output of 13112221.

You may assume that the element that was entered (and the element immediately 
after it) will be at most 1024 digits in length. The input will be a sequence 
of digits [1-3] and will be terminated by a single period character ('.').
Do not worry about error checking -- assume all inputs are correctly formatted.

====== SAMPLE RUN #1 ======
Enter the element: 1.
The next element in the sequence is: 11
===========================

====== SAMPLE RUN #2 ======
Enter the element: 111221.
The next element in the sequence is: 312211
===========================
 
 
====== SAMPLE RUN #2 ======
Enter the element: 13211311123113112211.
The next element in the sequence is: 11131221133112132113212221
===========================


*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
	char input[1024];      //The input element
	char result[1024];     //The next element
	int length = 0;        //The length of the input
	int resultLength = 0;  //The length of the result

	printf("Enter the element: ");
	do{
		scanf("%c", &input[length]);
	} while(input[length++] != '.');	
	length -= 1; //Decrement length because of the length++ that just executed
	
	//Go through the loop, adding to the result each time the chain stops
	char prev = input[0];   //The previous character
	int count = 1;          //The count in the current chain
	int i, j = 0;           
	for(i=1; i < length; i++){
		if(input[i] == prev){
			count++;   //If the chain is unbroken, increment the count
		} else{
			//If the chain breaks, store the count and digit in result and reset count
			result[j++] = count + '0';
			result[j++] = prev;
			count = 1;
		}
	
		//Set prev to be the current digit
		prev = input[i];
	}	

	//Handle the last digit using the current value of count and prev
	result[j++] = count + '0';
	result[j++] = prev;

	//Print out the result
	printf("The next element in the sequence is: ");
	resultLength = j;
	for(i=0; i < resultLength; i++){
		printf("%c", result[i]);
	}
	printf("\n");

	system("pause");
	return 0;
}