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;
}
#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;
}