Logo  

CS256 - Fundamentals of Structured Programming

Description

Exercise #7

Read the comments in the C files and make them into working programs.

New functions you will need to learn about to complete this assignment:


#include <stdlib.h>

int atoi(char str[]);
  • Function that takes a string (that looks like a number, i.e. "123") and converts it into a real number. Examples:

    int num = atoi("123");
    int num = atoi(argv[1]);


#include <stdio.h>

int getchar(void);
  • Reads a character from the standard input (stdin) or EOF on end of input. Example:
int ch;
// Note that (ch = getchar()) is enclosed entirely in ()'s, that's important
// for the following test expression.  Reads a character and prints it until
// there is no more input.

while ((ch = getchar()) != EOF) {
  printf("%c", ch);
}

#include <stdio.h>

char *fgets(char *s, int size, FILE *stream);
  • Reads a line of text (characters terminated by a newline (\n) up to a maximum of "size" number of characters from the input "stream". Returns NULL when there is no more data to read.

    Example:

// Space for 1024 characters + 1 (for the null character):
char buf[1025];
// Make sure the string buffer "buf" is null terminated.
buf[1024] = '\0';

// Reads lines of up to 1024 characters in length, the newline is still a
// a part of the string.  Prints the string read:
while ( fgets(buf, 1024, stdin) != NULL) {
  printf("%s", buf);
}