Here is the code for the lab:
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[]="AAACTGACATTGGACCTACTTTGACT",
short_str[]="ACT";
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */ while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[]="AAACTGACATTGGACCTACTTTGACT",
short_str[]="ACT";
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */ while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
/* Exit program. */
return 0;
} /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/
The following is what was generated:
} /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/
The following is what was generated:
location 3
location 18
location 24
number of occurrences: 3



