Saturday, December 16, 2017

Lab 18***: Programming Pointers and STR Functions

CLASSWORK/ HOMEWORK:
DNA String 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;

printf("Long String: %s \n", long_str);
printf("Short String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("ptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
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;
}

/*????????????????????????????????????????????????????????????-*/

Part 1:
/*??????????????????????????????????????????????????????????????-*/
/* 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>
#define MAX_SIZE 1000
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];

printf("Enter in long DNA string: \n");
scanf("%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
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;
}

/*????????????????????????????????????????????????????????????-*/

Part 2:
/*??????????????????????????????????????????????????????????????-*/
/* 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>
#define MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

printf("\nptr1\t\tlong_str \n");
printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

Part 3:
/*??????????????????????????????????????????????????????????????-*/
/* 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>
#define MAX_SIZE 1000
#define FILENAME "dnastring.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *dna;
dna = fopen(FILENAME, "r");
if (dna == NULL)
printf("Error opening input file. \n");
else
{
fscanf(dna,"%s",long_str);

printf("Enter in short DNA string: \n");
scanf("%s",short_str);

char *ptr1=long_str, *ptr2=short_str;

int short_length = strlen(short_str);
int long_length = strlen(long_str);
if (short_length > long_length)
printf("Error: Short string must be smaller than long string.\n");
else
{
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
//for (int i=ptr1; i<=(ptr1+strlen(long_str)-1); i++)
//printf("Long String: %c",*(ptr1+i));

//printf("\nptr1\t\tlong_str \n");
//printf("%i\t\t%i \n", ptr1, long_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("%i\t\t%i \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(dna);
}
}
/* Exit program. */
return 0;
}

/*????????????????????????????????????????????????????????????-*/

No comments:

Post a Comment