好吧,如果你知道它是前三个,你总是可以这样做:
char *strand;
// load string in there
if (3 >= strlen(strand) && ( strncmp("aug",strand,3) == 0 )) {
// do stuff
}
很快,你得到了你的确认。
如果您需要处理不同的订单,也许您可以澄清您的问题。
编辑:
如果您需要其中之一:
char *strand;
// load string in there
if (3 >= strlen(strand) &&
( strncmp("aug",strand,3) == 0
|| strncmp("agu",strand,3) == 0
|| strncmp("gau",strand,3) == 0
|| strncmp("gua",strand,3) == 0
|| strncmp("uga",strand,3) == 0
|| strncmp("uag",strand,3) == 0 )
{
// do stuff
}
如果您需要这些字母的任何排列:
char *strand;
int matchCount = 0;
// load string in there
if (3 >= strlen(strand))
{
switch (strand[0])
{
case 'a':
case 'u':
case 'g':
matchCount++;
break:
}
switch (strand[1])
{
case 'a':
case 'u':
case 'g':
matchCount++;
break:
}
switch (strand[2])
{
case 'a':
case 'u':
case 'g':
matchCount++;
break:
}
}
if (3 == matchCount) {
// do stuff
}
好的,鉴于您的更新,这是您第二个问题的解决方案。
#define METHIONINE "aug"
#define PHENYLALAINE "uuu"
#define ISOLEUCINE "auu"
#define UNDEFINEDVALUE 0
#define METHIONINEVALUE 1
#define PHENYLALAINEVALUE 2
#define ISOLEUCINEVALUE 3
#define NUMBEROFACIDS 256
char *strand,*strandItr;
int aminoAcidList[NUMBEROFACIDS]={UNDEFINEDVALUE};
int aminoAcidCount = 0;
unsigned int i = 0;
// load string in there
if ((strlen(strand) % 3)) != 0)
{
// your string doesn't only have these three-long amino acids
}
aminoAcidCount = strlen(strand)/3;
for (i = 0; i < aminoAcidCount; i++)
{
if (strncmp(METHIONINE,(strand + i*3),3) == 0)
{
aminoAcidList[i] = METHIONINEVALUE;
}
else if (strncmp(PHENYLALAINE,(strand + i*3),3) == 0)
{
aminoAcidList[i] = PHENYLALAINEVALUE;
}
else if (strncmp(ISOLEUCINE,(strand + i*3),3) == 0)
{
aminoAcidList[i] = ISOLEUCINEVALUE;
}
}
// do other stuff
for (i = 0; i < aminoAcidCount; i++)
{
switch (aminoAcidList[i])
{
case METHIONINEVALUE:
printf ("Methionine\n");
break;
case PHENYLALAINEVALUE:
printf ("Phenylalaine\n");
break;
case ISOLEUCINEVALUE:
printf ("Isoleucine\n");
break;
case UNDEFINEDVALUE:
default:
printf ("Unknown amino acid\n");
break;
}
}