PDA

View Full Version : Finding Substrings in C


wizzard21
02-26-2007, 07:11 PM
Hello, i am having a problem with finding substrings in C. I have my program retrieving the date for the mother's genes and i have a 2D array to obtain the babies genes. I keep getting an error however and i can't seem to fix it. What i am trying to do is search one specific gene in the baby, and check to see whether the mother has it. for example does mother contain input[k][l] where "k" is the baby and "l" is the letter of the baby. Attached below is my code. Any help would be of the greatest assistance. Any questions or clarifications let me know. Thank you.

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

void strip_newline( char *str, int size )/*fucntion to strip the newline*/
{
int j;
for ( j = 0; j < size; ++j )
{
if ( str[j] == '\n' )
{
str[j] = '\0';
return;
}
}
}


int main()
{
/*declarations of variables*/
char mother[10];
char father[10];
char input[10][5];
int length;
int i,x,k,l,n;

fgets(mother,11,stdin);/* retreives mothers genes*/
strip_newline(mother, 11);/*strips newline*/
fgets(father,11,stdin);/*retreives fathers genes*/
strip_newline(father, 11);/*strips newline*/

scanf("%d\n", &x);/*retreives # of babies*/

for(i=0;i<x;i++){/*loop to retreive babies*/
fgets( input[i], 6, stdin);/*retreives babies*/
strip_newline(input[i], 6);/*strips newline from them*/
}

for(k=0;k<x;k++){/*loops babies*/
length = strlen(input[k]);/*retreives length of the baby*/
printf("%d\n",length);/*test to make sure the length is right*/

for(l=0;l<length;l++){/*loops letter in baby*/

if(isupper(input[k][l])){ /*checks if it is uppercase*/


printf("up\n");/*test to prove that it is uppercase*/


if(strstr(mother,input[k][l])!=NULL)/*PROBLEM AREA*/
{
printf("test\n");
}


}/*close isupper*/
if(islower(input[k][l])){ /*checks if it is lowercase*/

printf("low\n");

} /*close islower*/

}/*close l*/
}/*close k*/

system("PAUSE");
return 0;
}

Gox
02-27-2007, 05:31 AM
Based on your description I have only a vague idea of what you're trying to do, however, I have edited your code so that it doesn't segfault. (I also made it print out descriptions of what input it was waiting for)

The segfault issue was with the second parameter of strstr. strstr requires that you pass char *'s which by default input is not. A slight change to &input[][] solved this issue. See code below. I also added a fgetc after the scanf for the # of babies as the return character seemed to still be in the stdin buffer, so this line removes it. I've commented the 2 lines I've added/edited.

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

void strip_newline( char *str, int size )/*fucntion to strip the newline*/
{
int j;
for ( j = 0; j < size; ++j )
{
if ( str[j] == '\n' )
{
str[j] = '\0';
return;
}
}
}


int main()
{
/*declarations of variables*/
char mother[10];
char father[10];
char input[10][5];
int length;
int i,x,k,l,n;

printf("Enter Mothers Genes\n");
fgets(mother,11,stdin);/* retreives mothers genes*/
strip_newline(mother, 11);/*strips newline*/

printf("Enter Fathers Genes\n");
fgets(father,11,stdin);/*retreives fathers genes*/
strip_newline(father, 11);/*strips newline*/

printf("Enter # of Babies\n");
scanf("%d", &x);/*retreives # of babies*/
fgetc(stdin); //Clear stdin of the return character left by scanf

for(i=0;i<x;i++){/*loop to retreive babies*/
printf("Enter Baby %d's Genes\n", i);
fgets( input[i], 6, stdin);/*retreives babies*/
strip_newline(input[i], 6);/*strips newline from them*/
}

for(k=0;k<x;k++){/*loops babies*/
length = strlen(input[k]);/*retreives length of the baby*/
printf("Length of baby genes %d\n",length);/*test to make sure the length is right*/

for(l=0;l<length;l++){/*loops letter in baby*/

if(isupper(input[k][l])){ /*checks if it is uppercase*/
printf("up\n");/*test to prove that it is uppercase*/

/*Need to pass a pointer to a string, added '&' infront of input*/
if(strstr(mother, &input[k][l])!=NULL)/*PROBLEM AREA*/
{
printf("test\n");
}


}/*close isupper*/

if(islower(input[k][l])){ /*checks if it is lowercase*/
printf("low\n");
} /*close islower*/

}/*close l*/
}/*close k*/

system("PAUSE");
return 0;
}


Good Luck,
Gox