Assessment Tool

Lecture 19:  Strings

Content Tested:  Writing a program (using string library)

Lecture Content:

Goals:

Assessment Technique:  Programming Activity

Purpose:

This planning and programming activity serves to give students experience writing a program that uses functions in the string library.

Activity:



We saw in today's lecture the use of a new concept called strings.  You will have a chance to use this new concept as well as functions from the string library.

Here's your task:  Write a program that takes nouns and displays their plural form according to these rules:
1.  If the noun ends in "y", remove the "y" and add "ies".
2.  If the noun ends in "s", "ch", or "sh" add "es".
3.  In all other cases just add "s".

Your program should prompt the user to enter a noun and display the plural form.  If the user enters the word "done", then the program should terminate.  You may assume each entered word is less than 48 characters long.

After you have finished writing the code, be sure to test your program (hand trace) on several nouns.  Some examples include "chair", "dairy", "boss", "science", "circus", "computer", "church", and "dish".

Be sure that when planning your solution, you make use of the string library functions.

Just like the other exercises, it is important to proceed with the following steps:  program specification, the algorithm and basic design of the program, and then the implementation of the C code.

Problem Specification:
 
 
 
 
 

The Algorithm:
 
 
 
 
 
 
 

The C Program:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Possible Solution

#include <stdio.h>
#include <string.h>

#define MAX_WORD 50

/* prototypes */
void pluralize (char word[]);

int main (void) {
 
  /* declarations */
  char temp_word[MAX_WORD];   /* stores temporary word entered by user */
 
  /* prompt user to enter noun */
  printf("This program prints the plural form of a noun.\n");
  printf("Please type in a noun in singular form: ");
  scanf("%s", temp_word);

  while (strcmp(temp_word, "done") != 0) {
 
    /* convert noun into plural form */
    pluralize (temp_word);

    /* print the plural form of the word */
    printf("The plural form is %s\n", temp_word);

    /* prompt user to enter noun */
    printf("Please type in a noun in singular form: ");
    scanf("%s", temp_word);
 
  }

  /* return successfully */
  return 0;
}

/* pluralize
 * parameters: char word[]
 * return values:  none, but word[] will be changed through this function
 * functionality:  changes word[] to be the plural form of word[]
 */

void pluralize (char word[]){
 
  /* declarations */
  int length;

  /* find length of word */
  length = strlen(word);

  /* check first rule:  if word ends in "y" then change to "ies" */
  if (word[length - 1] == 'y') {
    word[length - 1] = 'i';
    word[length] = 'e';
    word[length + 1] = 's';
    word[length + 2] = '\0';   /* remember to put '\0' at end of string */
  }
 
  /* check second rule:  if word ends in "s" "ch" or "sh" add "es" */
  else if (word[length - 1] == 's' ||
    (word[length - 2] == 'c' && word[length - 1] == 'h') ||
    (word[length - 2] == 's' && word[length - 1] == 'h')){
    /* concatenate "es" to word */
    strcat(word, "es");
  }

  /* otherwise, just add "s" to the end of word */
  else {
    strcat(word, "s");
  }
}
 

Possible Uses of Activity: