RNA to protein. Program compiles but won't stop running - c++

I recently learned about multidimensional arrays and was given the task of analyzing strands of RNA and translating them into sequences of protein. I decided to use my knowledge of multidimensional arrays to create a definition of each amino acid a codon (group of 3 RNA bases) would translate to.
//RNA codon to amino acid mapping
char aminoAcid[4][4][4];
//A = 0, C = 1, G = 2, U = 3
//phenylalanine - F
aminoAcid[3][3][3] = 'F';
aminoAcid[3][3][1] = 'F';
//Leucine - L
aminoAcid[3][3][0] = 'L';
aminoAcid[3][3][2] = 'L';
//Serine - S
aminoAcid[3][1][3] = 'S';
aminoAcid[3][1][1] = 'S';
aminoAcid[3][1][0] = 'S';
aminoAcid[3][1][2] = 'S';
//tyrosine - Y
aminoAcid[3][0][3] = 'Y';
aminoAcid[3][0][1] = 'Y';
//stop codon
aminoAcid[3][0][0] = '-';
aminoAcid[3][0][2] = '-';
//cysteine - C
aminoAcid[3][2][3] = 'C';
aminoAcid[3][2][1] = 'C';
//stop codon
aminoAcid[3][2][0] = '-';
//tryptophan - W
aminoAcid[3][2][2] = 'W';
//leucine - L
aminoAcid[1][3][3] = 'L';
aminoAcid[1][3][1] = 'L';
aminoAcid[1][3][0] = 'L';
aminoAcid[1][3][2] = 'L';
//proline - P
aminoAcid[1][1][3] = 'P';
aminoAcid[1][1][1] = 'P';
aminoAcid[1][1][0] = 'P';
aminoAcid[1][1][2] = 'P';
//histidine - H
aminoAcid[1][0][3] = 'H';
aminoAcid[1][0][1] = 'H';
//glutamine - Q
aminoAcid[1][0][0] = 'Q';
aminoAcid[1][0][2] = 'Q';
//arginine - R
aminoAcid[1][2][3] = 'R';
aminoAcid[1][2][1] = 'R';
aminoAcid[1][2][0] = 'R';
aminoAcid[1][2][2] = 'R';
//isoleucine - I
aminoAcid[0][3][3] = 'I';
aminoAcid[0][3][1] = 'I';
aminoAcid[0][3][0] = 'I';
//methionine(start codon) - M
aminoAcid[0][3][2] = 'M';
//threonine -T
aminoAcid[0][1][3] = 'T';
aminoAcid[0][1][1] = 'T';
aminoAcid[0][1][0] = 'T';
aminoAcid[0][1][2] = 'T';
//asparagine - N
aminoAcid[0][0][3] = 'N';
aminoAcid[0][0][1] = 'N';
//lysine - K
aminoAcid[0][0][0] = 'K';
aminoAcid[0][0][2] - 'K';
//serine - S
aminoAcid[0][2][3] = 'S';
aminoAcid[0][2][1] = 'S';
//arginine - R
aminoAcid[0][2][0] = 'R';
aminoAcid[0][2][2] = 'R';
//valine - V
aminoAcid[2][3][3] = 'V';
aminoAcid[2][3][1] = 'V';
aminoAcid[2][3][0] = 'V';
aminoAcid[2][3][2] = 'V';
//alanine - A
aminoAcid[2][1][3] = 'A';
aminoAcid[2][1][1] = 'A';
aminoAcid[2][1][0] = 'A';
aminoAcid[2][1][2] = 'A';
//aspartic acid - D
aminoAcid[2][0][3] = 'D';
aminoAcid[2][0][1] = 'D';
//glutamic acid - E
aminoAcid[2][0][0] = 'E';
aminoAcid[2][0][2] = 'E';
//glycine - G
aminoAcid[2][2][3] = 'G';
aminoAcid[2][2][1] = 'G';
aminoAcid[2][2][0] = 'G';
aminoAcid[2][2][2] = 'G';
I created the following function to translate the strand. In this case, please note that my rna strand is:
AUGCUUAUUAACUGAAAACAUAUGGGUAGUCGAUGA
string rnaAnalysis::translateRna()
{
string protein = "";
int firstBase, secondBase, thirdBase;
for(int i = 0; i < newSequence.length() - 2; i+3)
{
if(newSequence[i] == 'A')
{
firstBase = 0;
}
else if(newSequence[i] == 'C')
{
firstBase = 1;
}
else if(newSequence[i] == 'G')
{
firstBase = 2;
}
else if(newSequence[i] == 'U')
{
firstBase = 3;
}
if(newSequence[i+1] == 'A')
{
secondBase = 0;
}
else if(newSequence[i+1] == 'C')
{
secondBase = 1;
}
else if(newSequence[i+1] == 'G')
{
secondBase = 2;
}
else if(newSequence[i+1] == 'U')
{
secondBase = 3;
}
if(newSequence[i+2] == 'A')
{
thirdBase = 0;
}
else if(newSequence[i+2] == 'C')
{
thirdBase = 1;
}
else if(newSequence[i+2] == 'G')
{
thirdBase = 2;
}
else if(newSequence[i+2] == 'U')
{
thirdBase = 3;
}
bool readSequence = true;
if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[0][3][2])
{
readSequence = true;
}
else if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][0] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][2] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][2][0])
{
readSequence = false;
}
if(readSequence)
{
protein = protein + aminoAcid[firstBase][secondBase][thirdBase] + " ";
}
else
{
continue;
}
}
return protein;
}
The bool is used for "start codons" and "stop codons", basically codons within the strand that will tell you when to record and when to stop. newSequence would be the RNA strand.
EDIT: I'm fairly new at this, so I understand my code may look really ugly. Any feedback on how to clean it up is much appreciated as well.

for(int i = 0; i < newSequence.length() - 2; i+3)
should be
for(int i = 0; i < newSequence.length() - 2; i += 3)
Your code never changes the value of i which is why it never stops running.
Your loop starts with the same piece of code three times, where you convert the letter to the 'base index'. That's an obvious place to use a function
for (int i = 0; i < newSequence.length() - 2; i += 3)
{
int firstBase = baseIndex(newSequence[i]);
int secondBase = baseIndex(newSequence[i + 1]);
int thirdBase = baseIndex(newSequence[i + 2]);
...
I'll leave you to write the baseIndex function.

Related

Compression in c++ using bitwise operators

We have been given an assignment in which we have to compress 4 bytes into 3 bytes. It wants us to have a compression of 25%, by packing chars into 6 bits instead of 8 bits. It should be 25% exact compression, but my program is doing approx 50%. Code Book is my own "ASCII" sort of table, The bitwise operators are used to perform compression. Does anyone have any idea why it is compressing it by 50% rather than 25%? I know namespace std isn't a good practice, but we are asked to use it. Thanks!`
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned char CodeBook[53][2];
unsigned char FindCharacterCode(unsigned char C)
{
for (int j = 0; j < 53; j++)
if (CodeBook[j][0] == C)
return CodeBook[j][1];
return 0;
}
void MyCodeBook()
{
CodeBook[0][0] = 'a'; CodeBook[0][1] = 1;
CodeBook[1][0] = 'b'; CodeBook[1][1] = 2;
CodeBook[2][0] = 'c'; CodeBook[2][1] = 3;
CodeBook[3][0] = 'd'; CodeBook[3][1] = 4;
CodeBook[4][0] = 'e'; CodeBook[4][1] = 5;
CodeBook[5][0] = 'f'; CodeBook[5][1] = 6;
CodeBook[6][0] = 'g'; CodeBook[6][1] = 7;
CodeBook[7][0] = 'h'; CodeBook[7][1] = 8;
CodeBook[8][0] = 'i'; CodeBook[8][1] = 9;
CodeBook[9][0] = 'j'; CodeBook[9][1] = 10;
CodeBook[10][0] = 'k'; CodeBook[10][1] = 11;
CodeBook[11][0] = 'l'; CodeBook[11][1] = 12;
CodeBook[12][0] = 'm'; CodeBook[12][1] = 13;
CodeBook[13][0] = 'n'; CodeBook[13][1] = 14;
CodeBook[14][0] = 'o'; CodeBook[14][1] = 15;
CodeBook[15][0] = 'p'; CodeBook[15][1] = 16;
CodeBook[16][0] = 'q'; CodeBook[16][1] = 17;
CodeBook[17][0] = 'r'; CodeBook[17][1] = 18;
CodeBook[18][0] = 's'; CodeBook[18][1] = 19;
CodeBook[19][0] = 't'; CodeBook[19][1] = 20;
CodeBook[20][0] = 'u'; CodeBook[20][1] = 21;
CodeBook[21][0] = 'v'; CodeBook[21][1] = 22;
CodeBook[2][0] = 'w'; CodeBook[22][1] = 23;
CodeBook[23][0] = 'x'; CodeBook[23][1] = 24;
CodeBook[24][0] = 'y'; CodeBook[24][1] = 25;
CodeBook[25][0] = 'z'; CodeBook[25][1] = 26;
CodeBook[26][0] = '0'; CodeBook[26][1] = 27;
CodeBook[27][0] = '1'; CodeBook[27][1] = 28;
CodeBook[28][0] = '2'; CodeBook[28][1] = 29;
CodeBook[29][0] = '3'; CodeBook[29][1] = 30;
CodeBook[30][0] = '4'; CodeBook[30][1] = 31;
CodeBook[31][0] = '5'; CodeBook[31][1] = 32;
CodeBook[32][0] = '6'; CodeBook[32][1] = 33;
CodeBook[33][0] = '7'; CodeBook[33][1] = 34;
CodeBook[34][0] = '8'; CodeBook[34][1] = 35;
CodeBook[35][0] = '9'; CodeBook[35][1] = 36;
CodeBook[36][0] = '!'; CodeBook[36][1] = 37;
CodeBook[37][0] = '$'; CodeBook[37][1] = 38;
CodeBook[38][0] = '('; CodeBook[38][1] = 39;
CodeBook[39][0] = ')'; CodeBook[39][1] = 40;
CodeBook[40][0] = '#'; CodeBook[40][1] = 41;
CodeBook[41][0] = '&'; CodeBook[41][1] = 42;
CodeBook[42][0] = '%'; CodeBook[42][1] = 43;
CodeBook[43][0] = '-'; CodeBook[43][1] = 44;
CodeBook[44][0] = '.'; CodeBook[44][1] = 45;
CodeBook[45][0] = ','; CodeBook[45][1] = 46;
CodeBook[46][0] = '\''; CodeBook[46][1] = 47;
CodeBook[47][0] = ';'; CodeBook[47][1] = 48;
CodeBook[48][0] = ':'; CodeBook[48][1] = 49;
CodeBook[49][0] = '?'; CodeBook[49][1] = 50;
CodeBook[50][0] = ' '; CodeBook[50][1] = 51;
CodeBook[51][0] = ' '; CodeBook[51][1] = 52;
CodeBook[52][0] = '¶'; CodeBook[52][1] = 53;
}
int main()
{
MyCodeBook();
ifstream In;
ofstream Out;
In.open("C://Users//osama//Desktop//code.txt");
Out.open("C://Users//osama//Desktop//compressed.txt");
unsigned char Data[4] = { 0 },
Compressed[3] = { 0 }, Code[4] = { 0 };
int i;
while (!In.eof())
{
if (In.is_open())
{
for (i = 0; i < 4; i++)
{
In >> Data[i];
Code[i] = FindCharacterCode(Data[i]);
}
}
else
{
cout << "Not open!" << endl << endl;
return -1;
}
Compressed[0] = Code[0] << 2;
Compressed[0] = (Compressed[0] | Code[1] >> 4);
Compressed[1] = (Code[1] << 4);
Compressed[1] = (Compressed[1] | Code[2] >> 2);
Compressed[2] = (Code[2] << 6);
Compressed[2] = Compressed[2] | Code[3];
for (i = 0; i < 3; i++)
{
Out << Compressed[i];
}
}
In.close();
Out.close();
return 0;
}
Hint:
For any given input of your compression algorithm you will get a number between 1 and 53 (let's call it 0 and 53).
How many bits do we need to fit a number this large?
1? in one bit we can store between 0 and 1 - too small.
2? in 2 bits we can store between 0 and 3 - too small.
...
5? in 5 bits we can store between 0 and 31 - too small.
6? in 6 bits we can store between 0 and 63 - big enough.
the inputs are chars, or bytes - 8 bits.
6 / 8 * 100 = 75%
Here's your 25% compression.
Now you need to figure out how to encode 4 lots of 6 bits into 3 bytes of 8 bits.
That's where your bitwise operators (and some bit-shifting) will come in.

getline() isn't working in code

Yesterday I started developing a encrypting machine, that works like a Caesar cipher. You put a message (e.g., HELLO), choose a key value (e.g., 3) and the result is KHOOR (3 letters forward).
The problem is that, if I use "cin >> msg;" I can only codify one word. If I use "getline (cin, msg);", the code doesn't work. Maybe it's a simple problem, but I can't solve it... :(
string msg;
int a, b, i, key_value;
char c;
cout << "WRITE YOUR MESSAGE:" << endl;
cin >> msg; //HERE IS THE PROBLEM!!!
system ("cls");
cout << "PUT A KEY VALUE:" << endl;
cin >> key_value;
system ("cls");
cout << "THE CODIFIED MESSAGE IS:" << endl;
for (i=0; i < msg.length(); i++) {
if (msg[i] == 'A') a = 1;
if (msg[i] == 'B') a = 2;
if (msg[i] == 'C') a = 3;
if (msg[i] == 'D') a = 4;
if (msg[i] == 'E') a = 5;
if (msg[i] == 'F') a = 6;
if (msg[i] == 'G') a = 7;
if (msg[i] == 'H') a = 8;
if (msg[i] == 'I') a = 9;
if (msg[i] == 'J') a = 10;
if (msg[i] == 'K') a = 11;
if (msg[i] == 'L') a = 12;
if (msg[i] == 'M') a = 13;
if (msg[i] == 'N') a = 14;
if (msg[i] == 'O') a = 15;
if (msg[i] == 'P') a = 16;
if (msg[i] == 'Q') a = 17;
if (msg[i] == 'R') a = 18;
if (msg[i] == 'S') a = 19;
if (msg[i] == 'T') a = 20;
if (msg[i] == 'U') a = 21;
if (msg[i] == 'V') a = 22;
if (msg[i] == 'W') a = 23;
if (msg[i] == 'X') a = 24;
if (msg[i] == 'Y') a = 25;
if (msg[i] == 'Z') a = 26;
b = a + key_value;
if (b > 26) b -= 26;
if (b == 1) c = 'A';
if (b == 2) c = 'B';
if (b == 3) c = 'C';
if (b == 4) c = 'D';
if (b == 5) c = 'E';
if (b == 6) c = 'F';
if (b == 7) c = 'G';
if (b == 8) c = 'H';
if (b == 9) c = 'I';
if (b == 10) c = 'J';
if (b == 11) c = 'K';
if (b == 12) c = 'L';
if (b == 13) c = 'M';
if (b == 14) c = 'N';
if (b == 15) c = 'O';
if (b == 16) c = 'P';
if (b == 17) c = 'Q';
if (b == 18) c = 'R';
if (b == 19) c = 'S';
if (b == 20) c = 'T';
if (b == 21) c = 'U';
if (b == 22) c = 'V';
if (b == 23) c = 'W';
if (b == 24) c = 'X';
if (b == 25) c = 'Y';
if (b == 26) c = 'Z';
cout << c;
}
So your code works fine: http://ideone.com/lBhD78
If you're trying to accept more than 1 word the simple extraction operator will not work however. Which is probably what you're asking about. In this case you will want to use getline. Like this for instance:
getline(cin, msg, '\n');
So I'm not certain what you're complaining about, however this could be much improved by:
Being case-insensitive
Only modifying alpha-characters
You could accomplish this by doing something like:
transform(cbegin(msg), cend(msg), ostream_iterator<char>(cout), [&](unsigned char i){
if(isalpha(i)) {
const auto a = islower(i) ? 'a' : 'A';
i = (i - a + key_value) % 26 + a;
}
return i; });
I've written a Live Example complete with getline.
You have to be carefull to write questions.
BUT, without being clear what you mean, the answer is:
When you read cin>>s, by default, you are considering the data are separated by "white spaces". So, it skips spaces and read the string until a new space or end of input.
If you have problems with getline, probably the problem is before the code you are shown. If you read a value with, for example, cin>>integer, you push newline key at the end of the input, but this character is still in the stream. If the following line in your code es getline, probably you will read an empty line.
In the first item you find why you current code works, in the second one, why your getline-version doesn't.

Convert string of hexadecimal to decimal in c

I am writing an operating system in C and assembly, and in implementing the EXT2 file system I have encountered a problem. I need to convert FOUR bytes of hexadecimal to decimal in c. An example would be to convert 00 00 01(10000) to 65536.I need to convert to decimal,because parsing the super block requires all values to be in decimal. Most specifically the ext2 fs I'm working on is here:
#include "ext2.h"
#include <stdlib.h>
long hex2dec(unsigned const char *hex){
long ret = 0;
int i = 0;
while(hex[i] != 0){
//if(hex[i] >= 0x00 && hex[i] <= 0x09)
// ret+=(10 * i) * hex[i];
}
//kprintf("\n");
return ret;
}
char *strsep(char *buf,int offset,int num){
char *ret = malloc(1024);
int j = 0;
int i = offset;
int end = (offset + num);
int i1 = 0;
while(i1 < num){
///kstrcat(ret,&buf[i]);
ret[i1] = buf[i];
i++;
i1++;
}
return ret;
}
int get_partition(partnum){
if(partnum > 4)
return -1;
//int i = (12 * partnum);
int i = 0;
if(partnum == 1)
i = 190;
else if(partnum == 2)
i = 206;
else if(partnum == 3)
i = 222;
else
i = 190;
int ret = 0;
char *buf = malloc(1024);
ata_read_master(buf,1,0x00);
ret = buf[(i + 2)];
return ret;
}
int _intlen(int i){
int ret = 0;
while(i){
ret++;
i/=10;
}
return ret;
}
int _hex2int(char c){
if(c == '0')
return 0;
else if(c == '1')
return 1;
else if(c == '2')
return 2;
else if(c == '3')
return 3;
else if(c == '4')
return 4;
else if(c == '5')
return 5;
else if(c == '6')
return 6;
else if(c == '7')
return 7;
else if(c == '8')
return 8;
else if(c == '9')
return 9;
else if(c == 'A')
return 10;
else if(c == 'B')
return 11;
else if(c == 'C')
return 12;
else if(c == 'D')
return 13;
else if(c == 'E')
return 14;
else if(c == 'F')
return 15;
}
int hex2int(char c){
int i = c;
}
int comb(const char *str,int n){
int i = 0;
int ret = 0;
while(i < n){
//if(str[i] == 0x01)
// kprintf("(:");
/*int j = str[i];
int k = 0;
int m = 0;
if(j < 10)
j*=10;
else
while(j > 0){
k+=(10 ^ (_intlen(j) - m)) * j % 10;
m++;
j/=10;
}
//kprintf("%d",j);
//if(j == 1)
// kprintf("(:");*/
i++;
}
//ret = (char)ret;
ret = (char)str
int ret = 0;
int i = 0;
char *s = malloc(1024);
/*while(i < n){
//kstrcat(s,&((char*)buf[i]));
n++;
}*/
return ret;
//kprintf("\n");
//return ret;
}
struct ext2_superblock *parse_sblk(int partnum){
int i = get_partition(partnum);
if(i > 0)
kprintf("[EXT2_SUPERBLOCK]Found partition!\n");
else
i = 0;
struct ext2_superblock *ret;
struct ext2_superblock retnp;
char *buf = malloc(1024);
int i1 = 0;
//char *tmpbuf = malloc(4);
/*if(i != 0)
ata_read_master(buf,((i * 4)/256),0x00);
else{
kprintf("[WRN]: Looking for superblock at offset 1024\n");
ata_read_master(buf,4,0x00);
}*/
ata_read_master(buf,2,0x00);
const char *cmp = strsep(buf,0,4);
retnp.ninode = comb(strsep(buf,0,4),4);
retnp.nblock = comb(strsep(buf,4,4),4);
retnp.nsblock = comb(strsep(buf,8,4),4);
retnp.nunallocb = comb(strsep(buf,12,4),4);
retnp.nunalloci = comb(strsep(buf,16,4),4);
retnp.supernum = comb(strsep(buf,20,4),4);
retnp.leftshiftbs = comb(strsep(buf,24,4),4);
retnp.leftshiftfs = comb(strsep(buf,28,4),4);
retnp.numofblockpg= comb(strsep(buf,32,4),4);
// retnp.numofffpbg= comb(strsep(buf,36,4));
retnp.numoffpbg = comb(strsep(buf,36,4),4);
retnp.numofinpbg = comb(strsep(buf,40,4),4);
retnp.lastmount = comb(strsep(buf,44,4),4);
retnp.lastwrite = comb(strsep(buf,48,4),4);
retnp.fsckpass = comb(strsep(buf,52,2),2);
retnp.fsckallow = comb(strsep(buf,54,2),2);
retnp.sig = comb(strsep(buf,56,2),2);
retnp.state = comb(strsep(buf,58,2),2);
retnp.erroropp = comb(strsep(buf,60,2),2);
retnp.minorpor = comb(strsep(buf,52,2),2);
retnp.ptimefsck = comb(strsep(buf,64,4),4);
retnp.inter = comb(strsep(buf,68,4),4);
retnp.osid = comb(strsep(buf,72,4),4);
retnp.mpv = comb(strsep(buf,76,4),4);
retnp.uid = comb(strsep(buf,80,2),2);
retnp.gid = comb(strsep(buf,82,2),2);
ret = &retnp;
return ret;
i1 = 0;
}
If there is anyway of avoiding conversion and successfully implementing ext2 I would be glad to hear it. I would prefer it to be in c,but assembly is also okay.
If you have this:
const uint8_t bytes[] = { 0, 0, 1 };
and you want to consider that the bytes of a (24-bit) unsigned integer in little-endian order, you can convert to the actual integer using:
const uint32_t value = ((uint32_t) bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
This will set value equal to 65536.
You can use std::istringstream or sscanf instead of writing your own.
char const * hex_text[] = "0x100";
const std::string hex_str(hex_text);
std::istringstream text_stream(hex_str);
unsigned int value;
text_stream >> std::ios::hex >> value;
std::cout << "Decimal value of 0x100: " << value << "\n";
Or using sscanf:
sscanf(hex_text, "0x%X", &value);
std::cout << "Decimal value of 0x100: " << value << "\n";
A good idea is to search your C++ reference for existing functions or search the internet, before writing your own.
To roll your own:
unsigned int hex2dec(const std::string& hex_text)
{
unsigned int value = 0U;
const unsigned int length = hex_text.length();
for (unsigned int i = 0; i < length; ++i)
{
const char c = hex_text[i];
if ((c >= '0') && (c <= '9'))
{
value = value * 16 + (c - '0');
}
else
{
c = toupper(c);
if ((c >= 'A') && (c <= 'Z'))
{
value = value * 16 + (c - 'A') + 10;
}
}
}
return value;
}
To convert to use C-style character strings, change the parameter type and use strlen for the length.

Why does my program only change some of the letters

So, I am trying to make a ROT13decoder, and this is what I have so far. Only some of the letters change though and I am not sure why. I'm very new to programming. I am just trying to figure out how to read in files, and write to files. So far that part works, but yeah it doesn't change all the letters in the original file, just some of them. I would really appreciate any feedback.
#include <iostream>
#include <fstream>
//the letters in the secretMessage file are "Lbh unir gb fgnl va funcr. Zl tenaqzbgure, fur fgnegrq jnyxvat svir zvyrf n qnl jura fur jnf 60. Fur’f 97 gbqnl naq jr qba’g xabj jurer gur uryy fur vf
//
//
//and this is what it outputs to the decodedMessage file "Lbh haie gb fgal ia fhace. Ml geaadmbghee, fhe fgaeged jalkiag fiie milef a dal jhea fhe jaf 60. Fhe’f 97 gbdal aad je dba’g kabj jheee ghe hell fhe if.
using namespace std;
int main(){
ofstream fout;
ifstream fin;
fin.open("secretMessage.txt");
fout.open("decodedMessage.txt");
char c = 0;
while (!fin.eof()){
c = fin.get();
if (c == 'a')c = 'n';
if (c == 'b')c = 'o';
if (c == 'c')c = 'p';
if (c == 'd')c = 'q';
if (c == 'e')c = 'r';
if (c == 'f')c = 's';
if (c == 'g')c = 't';
if (c == 'h')c = 'u';
if (c == 'i')c = 'v';
if (c == 'j')c = 'w';
if (c == 'k')c = 'x';
if (c == 'l')c = 'y';
if (c == 'm')c = 'z';
if (c == 'n')c = 'a';
if (c == 'o')c = 'b';
if (c == 'p')c = 'c';
if (c == 'q')c = 'd';
if (c == 'r')c = 'e';
if (c == 's')c = 'f';
if (c == 't')c = 'g';
if (c == 'u')c = 'h';
if (c == 'v')c = 'i';
if (c == 'w')c = 'j';
if (c == 'x')c = 'k';
if (c == 'y')c = 'l';
if (c == 'z')c = 'm';
if (c == 'A')c = 'N';
if (c == 'B')c = 'O';
if (c == 'C')c = 'P';
if (c == 'D')c = 'Q';
if (c == 'E')c = 'R';
if (c == 'F')c = 'S';
if (c == 'G')c = 'T';
if (c == 'H')c = 'U';
if (c == 'I')c = 'V';
if (c == 'J')c = 'W';
if (c == 'K')c = 'X';
if (c == 'L')c = 'Y';
if (c == 'M')c = 'Z';
if (c == 'N')c = 'A';
if (c == 'O')c = 'B';
if (c == 'P')c = 'C';
if (c == 'Q')c = 'D';
if (c == 'R')c = 'E';
if (c == 'S')c = 'F';
if (c == 'T')c = 'G';
if (c == 'U')c = 'H';
if (c == 'V')c = 'I';
if (c == 'W')c = 'J';
if (c == 'X')c = 'K';
if (c == 'Y')c = 'L';
if (c == 'Z')c = 'M';
cout << c;
if (!fin.eof())fout << c;
}
fin.close();
fout.close();
return 0;
}
Most letters in your example will be flipped twice. You either need to add a whole lot of 'else' commands or use a switch statement.
if (c == 'a')c = 'n';
else if (c == 'b')c = 'o';
There is a better, mathematical method; but I'll leave that as an exercise to the reader.
You have a logic error. If you start with "a" the first IF turns it into "n" and a later IF turns it back into "a".
Have a look at the switch statement.
you should not use if conditions everywhere as each of them will be checked as you have a new row. Suppose your character is l then your if statement changes the letter to y but the condition for y is also checked so the character is changed back to l. So in fact your code is not not changing some of the letters, it is changing them twice.
You should be able to cure this using else if for the subsequent entries so that when one is true the other ones don't get checked, or you could use a switch case.
Take a single letter encoded in ROT13. If you encode it twice, you end up with the original letter again, which makes encoding and decoding ROT13 practically the same.
Now take for example the letter 'a'. What you are doing above in your code is
if (c == 'a') c = 'n';
...
if (c == 'n') c = 'a';
So for the first half of the letters above (a to m), you're encoding twice, getting the same letter again. Only for the lower half of the letters the encoding is only done once.
One solution to this would be to change your statements to something like
if (c == 'a') { c = 'n'; continue; }
This would skip the rest of the code in case of a match and continue with the next loop iteration.

Confusing of "Access Violation "

I don't Understand with this case but this is really really important for me, Please Help me...
void __fastcall TForm1::Button4Click(TObject *Sender)
{
String masuk, keluar, kosong;
int i, x, j, n = 0;
masuk = Edit2->Text;
keluar = masuk;
kosong = " ";
n = 0;
x = 0;
mulai:
i = 1;
j = 0;
j = j + n;
i = i + j;
if (masuk[i] == 'a')
{
keluar[i] = 't';
}
else if (masuk[i] == 't')
{
keluar[i] = 'a';
}
else if (masuk[i] == 'c')
{
keluar[i] = 'g';
}
else if (masuk[i] == 'g')
{
keluar[i] = 'c';
}
else
{
Application->MessageBoxA("Masukan Anda Salah", "Peringatan", MB_OK | MB_ICONWARNING);
keluar = kosong;
goto end;
}
n = n + 1;
if (i < 10)
goto mulai;
else
goto end;
end:
Memo1->Text = keluar;
}
if I make masukan more than 10 (i<10 (10 as default value)), it is ok but if it less than 10, it will make message exception Class EAccessViolation..
Taking a shot in the dark, but I think what you're actually trying to do might be this. I'm assuming that you're taking a single string of 10 characters which represents one half of a genome and you're generating another string of the pair values.
void __fastcall TForm1::Button4Click(TObject *Sender)
{
String masuk, keluar;
masuk = Edit2->Text;
keluar = masuk;
char kosong = ' ';
for (int i=0; i < 10; i++)
{
switch(masuk[i]) {
case 'a':
keluar[i] = 't';
break;
case 't':
keluar[i] = 'a';
break;
case 'c':
keluar[i] = 'g';
break;
case 'g':
keluar[i] = 'c';
break;
default:
Application->MessageBoxA("Masukan Anda Salah", "Peringatan", MB_OK | MB_ICONWARNING);
keluar[i] = kosong;
break;
}
Memo1->Text = keluar;
}