Translate a string matrix[lines][4] into one[lines][2]

This is a discussion on Translate a string matrix[lines][4] into one[lines][2] within the C Programming forums, part of the General Programming Boards category; I have this project in which i have to take a unidimensional string and convert it into a bidimensional one. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Post Translate a string matrix[lines][4] into one[lines][2]

    I have this project in which i have to take a unidimensional string and convert it into a bidimensional one. If the number of characters is not multiple of 3 (i.e. rests 1 or 2), i have to copy and add the last character of the last line of the matrix once or twice. This part is also working. The problem is, i'm not being able to add a sequence of commands that translate the matrix line by line based on a chart i already have. Here's what i'm trying to do:

    Code:
    switch (resto) 
        {
            case 0:
                for(i=0;i<size;i++)//assembles the matrix
                {
                    aminoacidos[x][y]=nucleotideos[i];
                    y++;
                    if (y==3)
                    {
                        y=0;
                        x++;    
                    }
                }
                for(i=0;i<(size/3);i++)//translated matrix
                { 
                    if (strcmp(aminoacidos[i],"ttt")||(strcmp(aminoacidos[i],"ttc")))
                    {
                        printf("F");
                    }                
                    if ((strcmp(aminoacidos[i],"tta"))||(strcmp(aminoacidos[i],"ttg"))||(strcmp(aminoacidos[i], "ctt"))||(strcmp(aminoacidos[i], "cta"))||(strcmp(aminoacidos[i], "ctc"))||(strcmp(aminoacidos[i], "ctg")))
                    {
                        printf("L");
                    }          }
                break;
    Thanks in advance

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your question is unclear. However, I see a couple of problems in your posted code. Firstly, you are not zero-terminating the strings that you copy to aminoacidos and therefore cannot simply use strcmp with them. You could use strncmp instead. Secondly, since strcmp returns 0 when the strings are equal, your "or" statements will always be true since they are saying "if it's not equal to this or it's not equal to that, etc". (strncmp also returns 0 to indicate equality)

    So you want something like:
    Code:
                    if (strncmp(aminoacidos[i], "tta", 3) == 0 ||
                        strncmp(aminoacidos[i], "ttg", 3) == 0 ||
                        strncmp(aminoacidos[i], "ctt", 3) == 0 ||
                    ...
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Quote Originally Posted by oogabooga View Post
    Your question is unclear. However, I see a couple of problems in your posted code. Firstly, you are not zero-terminating the strings that you copy to aminoacidos and therefore cannot simply use strcmp with them. You could use strncmp instead. Secondly, since strcmp returns 0 when the strings are equal, your "or" statements will always be true since they are saying "if it's not equal to this or it's not equal to that, etc". (strncmp also returns 0 to indicate equality)

    So you want something like:
    Code:
                    if (strncmp(aminoacidos[i], "tta", 3) == 0 ||
                        strncmp(aminoacidos[i], "ttg", 3) == 0 ||
                        strncmp(aminoacidos[i], "ctt", 3) == 0 ||
                    ...
    I understand what you're saying, but if i put only one condition inside the case structure, it translates perfectly well. When i add more than once, the program doesn't work as expected.

    example
    Code:
    ...if((strcmp(aminoacidos[i],"ttt"))||(strcmp(aminoacidos[i],"ttc")))
            printf("F");
    ...
    it works as expected

    My intention is to read the matrix line by line and translate the lines according to these structures.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
    if((strcmp(aminoacidos[i],"ttt"))||(strcmp(aminoacidos[i],"ttc")))
        printf("F");
    The above code says:

    "If aminoacidos[i] is NOT equal to "ttt" OR aminoacidos[i] is NOT equal to "ttc"
    then print F"

    Obviously it MUST be not equal to one or the other (i.e., it can't be both at the same time), so the condition will ALWAYS be true.

    Try this:
    Code:
        switch (resto) 
        {
            case 0:
                for(i=0;i<size;i++)//assembles the matrix
                {
                    aminoacidos[x][y]=nucleotideos[i];
                    y++;
                    if (y==3)
                    {
                        y=0;
                        x++;    
                    }
                }
                for(i=0;i<(size/3);i++)//translated matrix
                { 
                    if (strncmp(aminoacidos[i], "ttt", 3) == 0 ||
                        strncmp(aminoacidos[i], "ttc", 3) == 0)
                    {
                        printf("F");
                    }
                    else if (strncmp(aminoacidos[i], "tta", 3) == 0 ||
                             strncmp(aminoacidos[i], "ttg", 3) == 0 ||
                             strncmp(aminoacidos[i], "ctt", 3) == 0 ||
                             strncmp(aminoacidos[i], "cta", 3) == 0 ||
                             strncmp(aminoacidos[i], "ctc", 3) == 0 ||
                             strncmp(aminoacidos[i], "ctg", 3) == 0)
                    {
                        printf("L");
                    }
                }
                break;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. D3D 9 Lines
    By yaya in forum Game Programming
    Replies: 4
    Last Post: 08-28-2009, 12:14 AM
  2. cin >> string // multiple lines
    By msshapira in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2009, 04:39 AM
  3. multiple lines in a string
    By Cpro in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2008, 12:00 PM
  4. String tokens and random lines
    By helloamuro in forum C Programming
    Replies: 2
    Last Post: 04-20-2008, 08:40 PM
  5. New lines
    By 6arredja in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2004, 12:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21