Strings

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

int main(void)
{
/* Declare and initialize variables. */
int count=0;
int choice = 0;
int l_l, l_s;
char long_str[80];
char short_str[79];
char *ptr1=long_str, *ptr2=short_str;
while (choice != 1 && choice != 2){
printf( "1. read from file \n OR 2. input longstring ");
scanf(" %i", &choice);
}
printf(" \n");
if( choice == 1) {
FILE *file;
file = fopen("longstring.txt", "r");
if(file){
fgets(long_str, 80, file);
}
fclose(file);
}
if (choice == 2){
printf("Print the long string: \t");
scanf("%s", &long_str); }

printf("Print short string: \t");
scanf("%s", &short_str);

l_l = strlen(long_str);
l_s = strlen(short_str);

printf("Original Long String: ");
for (int i =0;i<=l_l; i++){
printf("%c", long_str[i]);
if(((int)long_str[i]) < 97){
long_str[i] = (char)((int)long_str[i] +32);
}
}

/* printf("\n");

printf("New Long String: ");
for (int i =0;i<=l_l; i++){
printf("%c", long_str[i]);
} */

printf("\n");
printf("Original Short String: ");
for (int i = 0; i<l_s; i++){
printf("%c", short_str[i]);
if((int)short_str[i] < 97){
short_str[i] = (char)((int)short_str[i] +32); }
}
/* printf("\n");
printf(" New short String: ");
for (int i = 0; i<l_s; i++){
printf("%c", short_str[i]);} */
printf("\n");


if (l_s < l_l){


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. */
}

else if (l_s >= l_l || l_s <= 0 || l_l <= 0){
printf(" SIZE ERROR- long string length: %i short string length: %i \n", l_l, l_s);
printf("Error: please try again with appropriate lengths of strings");
}

return 0;

}


Comments

Popular posts from this blog

Seismic Code

BMP180 Code + Library to SD card