IPC144 Lab 9 - Strings (Murray Saul's Sections)

Weekly Tip: A character string needs one more element than the number of characters in the string. This extra element is there to hold the null byte that identifies the end of the string.

In this lab, you will work with strings and arrays of strings. As with the previous labs, if you get stuck, ask for help - that is what lab sessions are for.

Enter the following program and call it lab9a.c

   #include <stdio.h>
void getstr(char s[ ], int maxlen) {
int i = 0;
char c='?';
while (c != EOF && c != '\n') {
c = getchar( );
if (i < maxlen) { /* making sure that you do not exceed the
storage size of the array 's' */
s[i] = c;
s[i] = '\0'; /* adding null byte to 's' to make it a string */
int main( ) {
char string[41];
int rc;
printf("Enter a string (using gets): ");
printf("The string is: '%s'\n", string);
printf("Enter a string (using scanf %%s): ");
rc = scanf("%s%*c", string);
printf("The string is: '%s' and scanf returned %d\n", string, rc);
printf("Enter a string (using scanf %%40[^\\n]): ");
rc = scanf("%40[^\n]%*c", string);
printf("The string is: '%s' and scanf returned %d\n", string, rc);
printf("Enter a string (using our own function getstr): ");
getstr(string, 40);
printf("The string is: '%s'\n", string);
  1. This program uses different techniques for allowing the user to enter a character string. Run the program a few times, and make sure you understand what the code is doing. Try the following test cases:
       abcd  (a single string)

    abcd efgh (a single string)

    <Enter> (no characters, giving an empty string)

    Approximately 50 characters of random input
  2. Notice the following:

  3. In a file called lab9b.c, write the code for a C function called: 'MyFNatoi' that accepts a null-terminated character array 's' (of potentially any size), and a pointer to an integer '*value' that will be used to 'pass back' a value to a variable in main.

    This function must convert the string in 's' to its equivalent as an integer (without using ANY built-in C library functions. You may only use the <stdio.h> library).

    For example, if your function accepted a character array "12345", you would send back the value 12345 (as an integer) to main (see table below).

    To accomplish this, you will have to go through each array element and make sure that each is a valid character digit, all the while performing a summation (totalling) the values in the array.

    For example, you could evaluate an array of: "12345" as:
    + 40
    + 300
    + 2000
    + 10000
    To convert an ASCII digit from a character to a number, you can take advantage of the fact that character '9' minus character '0' is integer 9 (i.e. '9' - '0' = 9).

    If any character in the array 's' is not numeric (i.e. 0 to 9), then the entire string will be considered to be invalid and non-numeric, and you should 'pass back' the value 0.

    Here are some examples:
       character array ('s'):     integer pointer (*value):
    "abc123" 0
    "49" 49
    "678abc" 0
    "" 0
    "889123" 889123
    Below is a main( ) program to test your function. Add it to lab9.c so that the complete program works correctly:
       int main( ) {
    char numbers[5][10] = { "123456789", "0001234", "abc5xyz", "", "987654321" };
    int i, value;
    for(i=0; i<5; i++) {
    MyFNatoi(numbers[i], &value);
    printf("array: '%s' int value: %d\n", numbers[i], value);
    return 0;

Submission Requirements:

For Murray Saul's sections, please follow the submission commands (or character strings to type) followed by <ENTER> :

cc lab9a.c -o lab9a
abcd efgh

mv typescript lab9.lst

mail -s "144lab9" -c $USER@learn.senecac.on.ca murray.saul@senecac.on.ca < lab9.lst

The "script" command acts like a "tape recorder" that records your commands and output into a file (in this case "lab9.lst") when the user enters the command "exit".