Computer Science 1622
Computer Science II

Laboratory Assignment 7
Strings
Due at the end of Lab

Introduction

Strings are used to store a sequence of related characters. They are useful for many purposes, like storing names, mailing addresses, file names, etc.

Strings can be thought of as an array of characters with a null character ('\0', or ASCII 0) at the end. For instance, a string containing "this" would be stored as an array of five characters with values 't', 'h', 'i', 's', and '\0'. Often, it is not very useful to treat strings as an array so we treat them as pointers to characters (remember that in C, pointers and arrays are essentially the same). By treating them as character pointers we can use the dynamic allocation functions (malloc, calloc, realloc, and free) to allocate and free space for them on demand.

There are several ways we can set the value of a string. Three of the most common ways to do this are:

	char *string1 = "this is a string";
	char string2[23] = "this is another string";  /*  note we need 23 characters for the \0  */
	char string3[] = "yet another string";

You can also use a charachter pointer (char *) to point at an existing string. For example:

	char *string4;
	string4 = string1;

If you need to set the value of a string after you have declared it and the variable you are using is a character pointer, you can use the following syntax:

	string4 = "even more strings";
If you are using an array to store a string and wish to change the value (or even if you have a pointer to a character and have allocated space using malloc or calloc) you can do the following:

	strcpy(string2, "the last string");
In the above case you have to be sure you have enough space to store all of the letters in the string including the terminating null character ('\0').

C provides many standard functions to manipulate strings. Below is a list of the functions that are used in the program for this lab.

The Program

The program given below reads in a file containing 50 lines of text and stores them as an array of strings (test). It then loops through another file (the while loop), reading in a line of text at every iteration. Each line of text is read into the search_string variable. Your job is to add code in the loop to search through the array of text lines and print out what line (row) and column each search_string is found at. To do this you will use the strstr function.

The strstr function takes two string parameters. The second string parameter is the string to search for in the first string parameter. If the second parameter is found in the first, strstr returns a pointer to the second string in the first, otherwise NULL is returned. For example, given the following declarations:

strstr(string1, string2) will return a pointer to the 21st character in string1. However, strstr(string1, string3) will return NULL because there is no instance of string3 in string1.

You will need to download two files to complete this lab. Links to the files are given below. When Netscape asks you what to do with the files, save them to your data disk (or the hard drive if you do not have a disk today). The first file (lab7.dat) contains the lines of text to be searched and the second file (search.dat) contains the text to be searched for.

The Code

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

#define MAX_STRINGS	50

char *readLine(FILE *fp);

int main(void)
{
   char *test[MAX_STRINGS];
   char *search_string;
   char *ptr;
   FILE *fp;
   int i;
 
   /*
      Open text data file.
   */  
   fp = fopen("lab7.dat", "r");
   if(fp == (FILE *)NULL)
   {
      printf("error\n");
      return -1;
   }
   /*
      Read lines of text from the file.
   */
   for(i = 0; i < MAX_STRINGS; i++)
      test[i] = readLine(fp);
   /*
      Close file.
   */   
   fclose(fp);
   /*
      Open search data file.
   */
   fp = fopen("search.dat", "r");
   if(fp == (FILE *)NULL)
   {
      printf("error\n");
      return -1;
   }
   /*
      Read a string from the search file and find the first occurance in the
      array of strings.
   */
   while(strlen(search_string = readLine(fp)))
   {

      printf("Searching for \"%s\":\n",search_string);

      /*
         *************************************************************
         
           Enter your code here to search through the text lines 
	   contained in the array test for find the first occurence of
	   search_string.
         
         *************************************************************
      */
      
   
      /*
         Deallocate the search string.
      */
      free(search_string);
   }
   /*
      Close the search data file.
   */
   fclose(fp);
   /*
      Deallocate the text lines.
   */
   for(i = 0; i < MAX_STRINGS; i++)
      free(test[i]);
      
   return 0;
}

char *readLine(FILE *fp)
{
   char line[81];
   char *newstring;
   
   if(fscanf(fp, "%[^\n]", line) < 1) {
      newstring = calloc(1,sizeof(char));
      strcpy(newstring,"");
      return newstring;
   }
   fgetc(fp);
   
   newstring = calloc(strlen(line) + 1,sizeof(char));
   strcpy(newstring,line);
   return newstring;
}

What to turn in

Turn in a hardcopy of the final program with the output for the given files.