/*********************************************************************/
/* Author: B. Alex Bridges                                           */
/* File: exam1-3.c                                                   */
/* Class: CPSC-202 Lab Section 3                                     */
/* Project: Exam 1 - Question 3                                      */
/* Purpose: This program will read in a string containing digits,    */
/*          determine its integer equivalent, and echo the value.    */
/*********************************************************************/

/* INCLUDES */
#include <stdio.h>
#include <string.h>
#include <math.h>

/* FUNCTION PROTOTYPE(S) */
int f_StrToInt (char str_Value[]);


/*********************************************************************/
/* MAIN FUNCTION                                                     */
/*********************************************************************/
int main ()
{
	/* LOCAL VARIABLES */
	char str_Digits[5+1] = ""; /* STRING OF DIGITS */
	int int_Digits = 0; /* INTEGER OF DIGITS */
	
	/* FUNCTION */
	/* 0. BEGIN */
	printf ("\n");
	printf ("======================= \n");
	printf ("= Test 1 - Question 3 = \n");
	printf ("======================= \n");
	printf ("\n");
	
	/* INPUT */
	printf ("Please enter a string of digits: ");
	scanf ("%s", str_Digits);
	str_Digits[strlen (str_Digits)] = '\0'; /* TERMINATES STRING */
	printf ("\n"); /* BLANK LINE */
	
	/* STRING => INTEGER */
	int_Digits = f_StrToInt (str_Digits);

	/* OUPUT */
	printf ("Those digits as an integer are '%d'. \n", int_Digits);
	return (0); /* COMPLETED SUCCESSFULLY */
} /* main */


/*********************************************************************/
/* FUNCTION PROTOTYPE(S)                                             */
/*********************************************************************/
/*************************************************************************/
/* Purpose: Receives a string variable containing digits and returns its */
/*          integer equivalent.                                          */
/* Input: a string                                                       */
/* Output: an integer                                                    */
/* Assumptions: The entire string ONLY contains digits                   */
/*              (e.g. no negative sign)                                  */
/*************************************************************************/
int f_StrToInt (char str_Value[])
{
	/* LOCAL VARIABLES */
	int i_Str = 0; /* VALUE STRING'S ARRAY INDEX */
	int int_StrLngth = 0; /* THE LENGTH OF THE VALUE STRING */
	int int_PlaceHolder = 0; /* DIGITS' PLACE HOLDER */
	int int_Value = 0; /* THE INTEGER EQUIVALENT */
	
	/* FUNCTION */
	int_StrLngth = strlen (str_Value);
	
	for (i_Str = 0; i_Str < int_StrLngth; i_Str++)
	{
		int_PlaceHolder = (int) pow (10.0, (double) (int_StrLngth - (i_Str + 1)));
		
		if (str_Value[i_Str] == '1')
			int_Value += (1 * int_PlaceHolder);
		else if (str_Value[i_Str] == '2')
			int_Value += (2 * int_PlaceHolder);
		else if (str_Value[i_Str] == '3')
			int_Value += (3 * int_PlaceHolder);
		else if (str_Value[i_Str] == '4')
			int_Value += (4 * int_PlaceHolder);
		else if (str_Value[i_Str] == '5')
			int_Value += (5 * int_PlaceHolder);
		else if (str_Value[i_Str] == '6')
			int_Value += (6 * int_PlaceHolder);
		else if (str_Value[i_Str] == '7')
			int_Value += (7 * int_PlaceHolder);
		else if (str_Value[i_Str] == '8')
			int_Value += (8 * int_PlaceHolder);
		else if (str_Value[i_Str] == '9')
			int_Value += (9 * int_PlaceHolder);
		else if (str_Value[i_Str] == '0')
			int_Value += (0 * int_PlaceHolder);
	} /* for*/
	
	return (int_Value);	
} /* f_StrToInt */
