Monday, November 27, 2017

02 November '17- Day 19 - Programming Pointers and STR functions

Day 19: the class began with lab on dan sequencing and using character strings to be able to take inputs and find the number of occurrences and also the locations. Next came the modifications to the program which included printing out long and short strings, allow the user to enter short or long strings, read long strings from a file and input short, modify to use either lowercase or uppercase letters, and check the length of short string with long string.
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. */

return 0;
} /*
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-*/ 

The following is what was generated:


location 3
location 18
location 24
number of occurrences: 3

No comments:

Post a Comment