Assignment 2: Introducing C

Due: 5:00pm, Friday, September 5. Value: 25 pts.

In this assignment you will gain some practice with elementary C programming. A separate page details how you can compile a C program in the Linux lab, and it outlines where to go for C compilers for other computers.

Below is a program [text file] that reads a single line from the user and displays the number of characters in that line. For example, if the user enters the line “hello world” and then presses enter, the program displays the number 11 (five letters in each word, plus the space).

#include <stdio.h>

int main() {
    int count;
    char c;

    printf("Input: ");
    count = 0;
    c = getchar();
    while (c != '\n') {
        c = getchar();
    return 0;

Your job is to write four different programs based on this one, as described below.

Important: You may not use any built-in C functions except for getchar() and printf().

What to submit

You will have four separate files, one for each problem. Please use the file as named in the problem (e.g., spaces.c for Problem 1). All four files should be submitted through the Moodle course page.

As with all assignments, you may complete this assignment with a classmate; you would complete all four problems with the same classmate. Please include both partners' names in a comment at the top of each file.

Problem 1: spaces.c

In a file named spaces.c, write a program to count the number of spaces in the line typed by the user. Thus, for “hello world”, the output should be 1, since there is just one space between the two words.

Problem 2: square.c

(If you are using Eclipse, you will want to create an entirely separate project for each problem, since each problem involves defining a different version of main.)

In a file named square.c, write a program that assumes the user types a number below 10000 and displays the square of that number. For example, if the user types 250 and the Enter key, the program should respond with 62500.

The hardest part of this is not the squaring, but rather converting the sequence of digits typed by the user into a number (which you can then easily square). You might find the following pseudocode useful for your inspiration:

num ← 0
while there are more digits:
    num ← 10 · num + next digit
display num · num

You'll need to convert a digit character to its corresponding number to make this happen. You can legally treat a character as a number in an expression, as in “k = c;” where k is an int and c is a char. But its ASCII code will be used, so if c holds the digit '1', k will receive its ASCII value, which is 49. Fortunately, the ASCII code assigns the digits 0 through 9 successive values from 48 ('0') through 57 ('9'), and so you can perform this conversion by simply subtracting 48 from the character value.

Problem 3: words.c

In a file named words.c, write a program that counts the number of words in the line typed by the user, defined as the number of sequences of non-space characters — or, equivalently, the number of times a non-space character is preceded either by a space or by the beginning of the line. Here are some sample inputs and the corresponding outputs.

hello world2
hello   world2
4 words - right?4
   word   word   2
” (empty string)0

Problem 4: sum.c

In a file named sum.c, write a program that assumes the user types several numbers separated by single spaces and displays the sum of those numbers. For instance, given the input “54 23 10”, the output should be 87. You can assume that there are no more than 10 numbers, each of which is less than 1,000,000.