CS 2073 Section 2, Spring 2007
Assignment 1: Type-in Program

For this assignment, you will:

This assignment is due at midnight on Wednesday, January 31, 2007.

Here is the program you are to type in. Substitute your name where it says Your Name.

#include <stdio.h>
#include <unistd.h>

/*
 * Name: Your Name
 *
 * Class: CS 2073 section 2
 *
 * Purpose: 
 *    This program reads your name from standard input and prints
 * the decimal ASCII (American Standard Code for Information Interchange)
 * value for each letter.
 *
 * Input:
 *    A single string terminated by a carriage return.
 * 
 * Output:
 *    "Your name is" followed by the input string, then
 * "The ASCII values are:" followed by a space-separated list of
 * decimal integers representing the ASCII codes for each character
 * in the input string.
 */

void print_ascii (char *);

/*
 * main function
 * 
 * This is the main program.  It asks for your name, reads it into an array
 * of characters, and calls the `print_ascii' function to print the ASCII
 * values.
 */
int main (void) {
        char    name[100];

        /* prompt for the name */

        printf ("Enter your name: ");

        /* read the name into the string `name' */

        fgets (name, 100, stdin);

        /* print the name and ASCII values */

        printf ("Your name is: %s", name);
        printf ("The ASCII values are: ");
        print_ascii (name);

        /* leave the program with exit code zero */

        exit (0);
}

/*
 * print_ascii (char *)
 *
 * This function accepts a string and prints the ASCII value of each
 * character, then prints a newline
 */
void print_ascii (char *s) {
        int     i, a;

        /* let i start at zero and continue until the i'th character
         * in s is null or carriage return, i.e., the end of the string
         */

        for (i=0; s[i] && (s[i] != '\n'); i++) {

                /* convert s[i] to an integer and print it, 
                 * followed by a space
                 */

                a = (int) s[i];

		/* notice that there's a space after %d here */

                printf ("%d ", a);
        }
        
        /* print a newline */

        printf ("\n");
}