首页 文章

检查char数组是否以某些characers开头[关闭]

提问于
浏览
1

我需要编写的代码有一个用户输入mRna strand,它存储在char数组中 . 这个mRNa链必须以字符'a','u','g'开头 . 我无法弄清楚如何验证这是否属实 . 我想要得到整数值的总和并测试它但我不能这样做因为它匹配可以输入的另外3个字母的字母 .

此外,每三个字母翻译成氨基酸 . 我不知道如何取每个三个字母并翻译它 .

我的第二个问题的一个例子mRNA字符串是auguuuauu

aug代码为蛋氨酸 .

uuu代码为苯丙氨酸 .

auu代码为异亮氨酸 .

因此,该程序将在每行3组中进行翻译并进行翻译 . 然后将值存储在字符数组中 . 我正在考虑这样做的方法是创建一个循环,一次将3个值存储到char数组中 . 我不知道如何使用char数组分析该值是什么,并将其更改为需要的值 . 一旦改变了我就会把它拖到最后 .

3 回答

  • 2

    好吧,如果你知道它是前三个,你可以随时这样做:

    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;
        }
    }
    
  • 0

    您可以使用strncmp(3)来比较两个字符串的开头:

    if (strncmp(mRNAstrand, "aug", 3) == 0)
    
  • 6

    对于你的第一个问题,请写:

    if (!(*mRna == 'a' || *mRna == 'u' || *mRna == 'g'))
        //Handle wrong array
    

    您应该为第二个问题提供一个代码示例,以便更清楚地了解您遇到的问题 .

相关问题