# Thread: Translate a string matrix[lines][4] into one[lines][2]

1. ## 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;```

2. 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 ||
...```

3. Originally Posted by oogabooga
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. 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;```