This question already has answers here:
Random name generator in c#
(8 answers)
Closed 1 year ago.
using System;
namespace npcnames
{
class Program
{
static string RandomVowel()
{
Random rand = new Random();
string[] Vowels = new string[5] { "a", "e", "i", "o", "u" };
int index = rand.Next(Vowels.Length);
string Vowel = Vowels[index];
return Vowel;
}
static string RandomConsonant()
{
Random rand = new Random();
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}
static string MaleName()
{
string malename = RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant();
return MaleName();
}
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName();
}
static void generateFemaleName(int from, int to, int step)
{
for (int a = from; a <= to; a = a + step)
{
Console.WriteLine("Female:");
Console.WriteLine(a + FemaleName());
}
}
static void generateMaleName(int from, int to, int step)
{
for (int b = from; b <= to; b = b + step)
{
Console.WriteLine("Male:");
Console.WriteLine(b + MaleName());
}
}
static void Main(string[] args)
{
generateFemaleName(1,10,1);
Console.WriteLine();
generateMaleName(1,10,1);
}
}
Hi guys I'm new to coding and everything, if anyone can help me how to resolve this issue I would really appreciate it. Problem is that in my code I keep getting stack overflow and I don't know how to prevent it to execute my program normally. The purpose of the program is to generate male and female names of the random selected vowels and consonants in the list of 10 for each.
The issue is here:
static string FemaleName()
{
string femalename = RandomVowel() + RandomConsonant() + RandomVowel() + RandomConsonant() + RandomVowel();
return FemaleName(); // <== bam!
}
Change:
return FemaleName();
to:
return femalename;
The short story is the method is continually calling itself. It's exactly the same for MaleName().
Also, while we are fixing things, Random initialization should be outside the method:
private static Random rand = new Random();
static string RandomConsonant()
{
string[] Consonants = new string[21] { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
int index = rand.Next(Consonants.Length);
string Consonant = Consonants[index];
return Consonant;
}
It will be fixed if you change
return MaleName();
to
return malename;
and the same for
return FemaleName();
to
return femalename;
It is the string that you want to return.
Good day,
So, I have a class that converts arabic numerals to roman numerals. I have a loop that executes, if the user selected "Arabisch"(arabic) in the dropdownlist.
If the user selected "römisch"(roman) I want to do the same loop but convert the numerals to roman. In following you can see the code of the both classes. Whats the best way to initialize the method of the "ArabicToRoman" class in my loop to convert the numbers to roman?
namespace Friedhof_Generator
{
class ArabicToRoman
{
private string[] Tausender =
{ "", "M", "MM", "MMM" };
private string[] Hunderter =
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner =
{ "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer =
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
private string ArabicRoman(int arabic)
{
if (arabic >= 4000)
{
int tausend = arabic / 1000;
arabic %= 1000;
return "(" + ArabicRoman(tausend) + ")" +
ArabicRoman(arabic);
}
string result = "";
int num;
num = arabic / 1000;
result += Tausender[num];
arabic %= 1000;
num = arabic / 100;
result += Hunderter[num];
arabic %= 100;
num = arabic / 10;
result += Zehner[num];
arabic %= 10;
result += Einer[arabic];
return result;
}
}
}
Above the ArabicToRoman class. Below the class with the loops.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Friedhof_Generator
{
public partial class frm_Grabnummer : Form
{
public frm_Grabnummer()
{
InitializeComponent();
ddl_Feld.Items.Add("Arabisch");
ddl_Feld.Items.Add("Römisch");
ddl_Feld.Items.Add("Alphabetisch");
ddl_Reihe.Items.Add("Arabisch");
ddl_Reihe.Items.Add("Römisch");
ddl_Reihe.Items.Add("Alphabetisch");
ddl_Nr.Items.Add("Arabisch");
ddl_Nr.Items.Add("Römisch");
ddl_Nr.Items.Add("Alphabetisch");
}
private void ddl_Feld_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btn_Start_Click(object sender, EventArgs e)
{
int FeldVon, FeldBis, ReiheVon, ReiheBis, NrVon, NrBis;
string Friedhof;
FeldVon = int.Parse(tb_Von_Feld.Text);
FeldBis = int.Parse(tb_Bis_Feld.Text);
ReiheVon = int.Parse(tb_Von_Reihe.Text);
ReiheBis = int.Parse(tb_Bis_Reihe.Text);
NrVon = int.Parse(tb_Von_Nr.Text);
NrBis = int.Parse(tb_Bis_Nr.Text);
Friedhof = tb_Friedhof.Text;
if (ddl_Feld.Text == "Arabisch")
{
for (int iFeld = FeldVon; iFeld <= FeldBis; iFeld++)
{
for (int iReihe = ReiheVon; iReihe <= ReiheBis; iReihe++)
{
for (int iNr = NrVon; iNr <= NrBis; iNr++)
{
lb_Ausgabe.Items.Add(string.Format("{0} - {1} - {2} - {3}", Friedhof, iFeld, iReihe, iNr));
}
}
}
}
else if(ddl_Feld.Text == "Römisch")
{
}
}
}
}
The ArabicToRoman class seems like a typical helper class, in days gone by you would just easily make it public and static
public static class ArabicToRoman
{
private static string[] Tausender = { "", "M", "MM", "MMM" };
private static string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private static string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private static string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public static string Convert(int arabic)
{
...
}
}
Usage
You can call this from anywhere that access to the assembly
ArabicToRoman.Convert(234);
You could also just make your classes and methods public
public class ArabicToRoman
{
private string[] Tausender = { "", "M", "MM", "MMM" };
private string[] Hunderter = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
private string[] Zehner = { "", "X", "XX", "XXX", "XL", "L", "LX" , "LXX", "LXXX", "XC" };
private string[] Einer = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
public string Convert(int arabic)
{
...
}
}
Usage
Once again you call this anywhere that can access the assembly it lives in
var converter = new ArabicToRoman();
converter.Convert(345);
However these days, there is a big push for Dependency Injection DI and Service architecture, You might make it a singleton Service and inject your converters. However this is debatable as well.
For your case and your level of understanding static classes or an instantiable class seem to be a good fit
Further Reading
static (C# Reference)
Use the static modifier to declare a static member, which belongs to
the type itself rather than to a specific object. The static modifier
can be used with classes, fields, methods, properties, operators,
events, and constructors, but it cannot be used with indexers,
finalizers, or types other than classes.
The Peptide Encoding Problem consists on Finding substrings of a genome encoding a given amino acid sequence.
The genetic code describes the translation of an RNA 3-mer (codon) into one of 20 different amino acids. The first three circles, moving from the inside out, represent the 1st, 2nd, and 3rd nucleotides of a given codon. The 4th, 5th, and 6th circles define the translated amino acid in three ways: the amino acid’s full name, its 3-letter abbreviation, and its single-letter abbreviation. Three of the 64 total RNA codons are stop codons, which halt translation and implicitly add a 21st stop symbol to the amino acid alphabet.
The problem consists on having
Input: A DNA string Text and an amino acid string Peptide.
and
Output: All substrings of Text encoding Peptide (if any such substrings exist).
Sample Input:
ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA
MA
Sample Output:
ATGGCC
GGCCAT
ATGGCC
the result is gotten by reading triplets so, ATGGCC encodes MA, GGCCAT inverse would be CCGGTA written backwards so it is in fact ATGGCC (CCGGTA.reverse)
the complement of genetic code is C-G, G-C, T-A and A-T
So to solve this problem I first use already defined arrays
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
char[] givenPeptideArray = givenPeptide.ToCharArray() ;
string codon="";
string auxCodon="";
string convertedCodon = "";
string clean = "";
int pivot = 0;
int foundFlag = 0;
string cleanReverse = "";
int foundFlagReverse = 0;
string convertedCodonReverse = "";
//so the idea I was working was to loop dna string until getting to 3*givenPeptide.Length
//because dna is red 3 by 3...
//then get givenPeptide.Length and compare each char of given peptide with encoded
//letter of scanned triplet
//so for example first givenpeptide is 'M' so I would search for 'ATG'
//then, if found increment a counter and then search for next 'GCC' etc...
//however When testing with longer string I do not get complete result
//I have
for (int pos = 0; pos < dna.Length - (3*givenPeptide.Length) ; pos++) {
pivot = pos;
for (int codonPos = 0; codonPos < givenPeptide.Length; codonPos++)
{
codon = dna.Substring(pivot, 3);
auxCodon = givenPeptideArray[codonPos].ToString();
convertedCodon = codonToAminoAcid(codon);
convertedCodonReverse = codonToAminoAcid(DNAComplement(codon));
if (auxCodon.Equals(convertedCodon) )
{
foundFlag++;
clean += codon;
}
else
{
foundFlag = 0;
clean = "";
}
if (foundFlag == givenPeptide.Length)
{
Console.WriteLine(clean);
foundFlag = 0;
clean = "";
}
//reverse
if (auxCodon.Equals(convertedCodonReverse))
{
foundFlagReverse++;
cleanReverse += codon;
}
else
{
foundFlagReverse = 0;
cleanReverse = "";
}
if (foundFlagReverse == givenPeptide.Length)
{
Console.WriteLine(cleanReverse);
foundFlagReverse = 0;
cleanReverse = "";
}
pivot+=3;
}
}
}//end main
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length ; k++)
{
if (CODONS[k].Equals (codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
}//end class
I should get
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAAGTATTCGAACCACATTACTAT
ATAATAATGCGGCTCGAATACTTCCTT
GTAGTAATGGGGCTCGAAAACCTCCTT
GTAGTAATGAGGTTCAAAAACCTCCTT
GTAGTAATGGGGTTCGAAGACTTCCTT
ATAATAGTGAGGCTCAAAAACTTCCTT
ATAGTAATGGGGTTCGAAGACTTCCTT
GTAGTAGTGCGGCTCAAAAACTTCCTT
ATAGTAATGAGGTTCGAAAACCTCTTT
ATAATAATGTGGCTCGAACACTTCTTT
GTAGTAATGGGGCTCAAACACCTCTTT
ATAGTAGTGAGGTTCGAAGACTTCCTT
GTAATAGTGCGGTTCAAAAACTTCCTT
ATAGTAGTGTGGTTCAAATACCTCCTT
However I am only getting:
AAGGAAGTATTTGAGCCTCATTATTAC
AAAGAGGTGTTTGAACCTCATTACTAT
AAGGAAGTGTTTGAACCTCACTATTAT
AAAGAAGTTTTCGAGCCGCACTACTAC
AAGGAGGTATTTGAACCCCACTATTAC
AAAGAAGTTTTCGAACCACATTATTAC
I was thinking on problem and It seems the problem is I am not looping all string each time...
It looks like the problem is two-fold. Your outer for loop doesn't go far enough to catch the very end of the dna string. Secondly, it appears that reversing individual codons doesn't work and that you have to take the entire (givenPeptideLength * 3) character string before getting the complement. The following changes output the strings you put in your example so I'm hopeful that it does what you want. It has the benefit of being much more concise as well.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Collections;
namespace DNA
{
class Program
{
private static string[] CODONS = {
"TTT", "TTC", "TTA", "TTG", "TCT",
"TCC", "TCA", "TCG", "TAT", "TAC", "TGT", "TGC", "TGG", "CTT",
"CTC", "CTA", "CTG", "CCT", "CCC", "CCA", "CCG", "CAT", "CAC",
"CAA", "CAG", "CGT", "CGC", "CGA", "CGG", "ATT", "ATC", "ATA",
"ATG", "ACT", "ACC", "ACA", "ACG", "AAT", "AAC", "AAA", "AAG",
"AGT", "AGC", "AGA", "AGG", "GTT", "GTC", "GTA", "GTG", "GCT",
"GCC", "GCA", "GCG", "GAT", "GAC", "GAA", "GAG", "GGT", "GGC",
"GGA", "GGG", };
private static string[] AMINOS_PER_CODON = {
"F", "F", "L", "L", "S", "S",
"S", "S", "Y", "Y", "C", "C", "W", "L", "L", "L", "L", "P", "P",
"P", "P", "H", "H", "Q", "Q", "R", "R", "R", "R", "I", "I", "I",
"M", "T", "T", "T", "T", "N", "N", "K", "K", "S", "S", "R", "R",
"V", "V", "V", "V", "A", "A", "A", "A", "D", "D", "E", "E", "G",
"G", "G", "G", };
private static string[] AMINO_ABBREVIATIONS = {
"F", "L", "I", "M", "V",
"S", "P", "T", "A", "Y", "H", "Q", "N", "K", "D", "E", "C", "W",
"R", "G" };
private static string[] FULL_NAMES = {
"phenylalanine", "leucine",
"isoleucine", "methionine", "valine", "serine", "proline",
"threonine", "alanine", "tyrosine", "histidine", "glutamine",
"asparagine", "lysine", "aspartic acid", "glutamic acid",
"cysteine", "tryptophan", "arginine", "glycine" };
public static string DNAComplement(string dna)
{
char[] array = dna.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
char let = array[i];
if (let == 'A') array[i] = 'T';
else if (let == 'T') array[i] = 'A';
else if (let == 'C') array[i] = 'G';
else if (let == 'G') array[i] = 'C';
}
Array.Reverse(array);
return new string(array);
}
public static string StringToAminoAcid(String input)
{
string result = "";
for (int i = 0; i < input.Length; i += 3)
{
result += codonToAminoAcid(input.Substring(i, 3));
}
return result;
}
public static string codonToAminoAcid(String codon)
{
for (int k = 0; k < CODONS.Length; k++)
{
if (CODONS[k].Equals(codon))
{
return AMINOS_PER_CODON[k];
}
}
// never reach here with valid codon
return "X";
}
static void Main()
{
//then input is DNA
string dna = "TTCGAATCCGTATTGCTTTGGTTGGTTGAGAGAGCGACCTACATTGCTAGTCAGAATAGGTGATTCACACAAAGCTTAGCACCTGGGCAGCACCCCGTGATGTAAACCTATGGGAACTAAGGGAGTCCTGCGGTTTTAGCCAGCAAGCGAGCCGGCAGGAACACTCATACCATCGGACGCGTTTGACGCCTCCCCGGAAAGGAAGTATTTGAGCCTCATTATTACGTATTGCCCGTTAGTCGACAAATCAAGCCCTCGTACGCAGCTTATTCGTACGACGTGGAGGCGTTCCCACGGGCCTAACACGATTGGAACACCACCATAGTAGTGTGGTTCAAATACCTCCTTTGGAGATCTAGAGCTTCACTCTGATTCTAGAGGCAACTTTACAATCGCTCTACGAAATTGTATGGACATCATCAACCGGATATTCTGGGGCGGTAGAATTTCTTTTGTTCGAATCGCTCTAGGCCAGGATCAAATTAATTGAATTGCGGACTCAAGGATCGCGATAGCCGACACATCGGACGCTGTAGAAAGCCAGTCTCTGGATTTAATCCACCCTCTATGTTTGACAAAGCACTAAAACGGGATAGTTTCGGGTGGTATAAGTTTCCCAAGACGATTGCATCGCAATTCATCAACAACCATGAACTTACTGTTTTAGTACTTCCACACACCTTGTTAAATTACGCCTTTACTTCATGTTGCGGTGTGTGTTAGATAGTGTGCAGCTACAAGTCTACCGCCATCGCAGCTCGGGATACCGGCAGATGAGATGGTCCTGAGCTCGTACCGGACTCAAACTTTTTCCTTTACTACCTAGGAATCGCCCATGCGAATTTGTCGGACACACACCATTACATTAACGTCACAACAGCTACTGTTAGAATTTTGCTCTTGCAAATCCTGGAAAGAGTTAAAAAAACTCTTCCGCGCGCCAATAGGGTAAATAATAGATAGCCAGACGGCTGTAAGAGGTGATGACATTTGCAACAATCATGCTGTCGCATCTTCCGCAAGTTCATGTCGCGCCTAGGCAATGGATCTGCGAATGGGGGCCACGGGGTATGAACTACGGAATTCTAAGAAAGTTGCCATCCAGAGTTAAGGGTTTGAGGCTAGTTGCATCGCTGGTAACGAACTACCTCATTACTTGGACGCGCAGTGTGACTTCACTCCTGTATAGCGATGATGCCAAGCAGGAATTAGCAAATCTGAAGAGCGTTTCCAAACTGGCCACTTGGACTGACACCTATCGCGGGGGATTTCAGCGCGTGTCGCTCTCACATGAGAGCTGCCGTCAGGAGCGGTAGAGTTTAGAGAGGAATGCGACAAACTCCCTATTCACCTCTCTGGTGATGTAAGGATATTTACGCTTAGTTCTATGCCAGGCTTAGGGCCTCTCGGAACTTTGGTGAGTCCTTATTAATTGATGCTACCTCTCCCTTACCTTCGCCCCAAGTCACGTAGAAGTACTCAATCCTGCTACATGATAATCAAATATTTCCAACGTTGGGAAATCGGTGACATCACATACTAGTTAAGAAACCACTGTCAGTGAACTTATATCCGGGGGAGAAAATCTACTAACTTACATACGCTGTGCGAGCAGTTTTCATTATAAGAAAATATACTCCCGAGGTACCGCATCAAGCACGACATTCCCGGAGAGCATAACATTTCGGTGCACCTGCTTTTGTGCGCTTGCTTGCGGTTATTTATAAACTACGCACAAGGCGCAAACCGCAGTGCGCATGTTTTCTCCGCCTGGCTAGAACTCGACATTCTCGTCAACGCCAATCTATGTGAGAGGATTTAGACCTCTGTGAAAACGAGTCCCTCTATAGAATAAATACCCAGATGCCAATGGGGGTTCTATCCGATGGCAGTGCATGGAGTGGTGGCTCCAGATTAAGGATGAGGAGAGGTAAAGATAACAGTTCGGTCGCCACGACGCGTTGCCAATCGAAATATCAGTACTAAAAGGCCCACCGCTCCGCTTTAGTCCGACTTTACATCCTGTGGAAATTGTCGAACGGAGGCTACATCGGGCTATATGAGTGTGAAAACCTATACTTCTCGCGTCGTTACTCAGTGCCGGTCTCCTGTTTCCCCCAGTCTTACGTACCCTTATTGATATTTGCTTCACGTTGAAACGTCCTAACGCAGCGTAAAGAGGTGTTTGAACCTCATTACTATAAAATCGCGATCGAAGGTAGACTACATACGCAAACGCCGAAACCCTCAGTTGGCCTTGTTGCAAGTATGGAACGTTGTAAAATTTTTCCTAGACGTTGAGCTATCGGTACAAGGTCGTTAGCGTCCTTACCCTTCACTTATATGCCCGACAAAACGCGGGTCCTAGTGCAGTGGTGGGAGCTTGGAATCCCGCAATACAAGGACAACCTGTATCTCGTTCGGCGTTCCGCGATCACTCGATCCCGAACCACTCCAAGCCTGGTTGATCAGCAAAAGCGGAAGGATGGATAAAGGGCTACTGGTTAATGGATGTAAACTTCCAATGATGAAATCCTGGAAACGAGGGATCGGGTTACGGTGGCGAACGGGGTACGGCAACGTGGCTATCTAGAGCCCGACGTTACGACTCATGTACATGCTGCTACGTGGTTGAAGCTGACGTTCAGATGAAGCAGTACTGAGTCCTAGGGCTTACTACTACTCCAATAGGTCTGGCCGGCCAGATACAAAAGTTCGTGGCGGCTCACCCCCTTTCTGGCGGGTGTAGCTTGCTGACCGGTTTGCTCGATAACACAGGCTAGCGAATAGTAATGAGGTTCGAAAACCTCTTTCCAACGACTGAAAGGGTCTACACGAACTATCTACATTTCCCCGCCCATGTCCTTCCGTCTGGTTGCTTCTGGAGATCCTTTCGCATTATACCGCAGCGTAGTGGCTCTGGCATATATGAAAAAATCCTTCTGTGGGTATTTGTGCCATCACTTATTGTTCGTACCGATATGGGATTACAAGTGCGATGTGATAATAAGCGAAGAAGCCAACATGTTACACTGTTCATGCGCTCCGGGTAATGTGCGGGCACCATGCTCAGTTCCCGCTCGCAGTTGTCACTGTCCCTGTTTCGGCACCATAATCAACATTTCCACGGCCACGCTGGTGAATAACCGAGGATACCGAAGTACAGCAAGAATGAGAGCGGGACTCCTCCATCTGCTTGTAATACGCCTTCAAGATAGTCCATAAAACGGTCGGGGTCTTGTTTCGGACTAGCCGCTTTGAAACGGTGCATAGTTGTGTCAAGTGTGGACATTGGCTTTCTATCCTCGTCAGCGATCCTCGGAAAGACTCGGGCAGTCGCCCCGAATCGTAATTAGGTAGTAGTGCGGCTCAAAAACTTCCTTCGACCTAACCGCTATAATGTTCGTAGATATAAATTTCGTTTCAGTATTAACAGGCGCACCGTATATATACGGAATGGTGTCGCCCCATTAGCTGCTCGCCAATATTTATCTAAGACCGCGCGCGTCTAGCGCCTTTAGTAGTTGCACCCGAGTATAGTAATGGGGTTCGAAGACTTCCTTCGCAAGGCTGCCATACTGTATCACAAGTACTGACGGAGCCCCGGAGGAGTGCAGGATACGGCAAAGGAGACCATTACCGGGGCATGAGTCCAAGTTAGCCCGTTAGGTGAAGGACGCTGATACAATAGTGAATCCGTTACTGAAAGGTTTAGAAGACCGGGGGGCTCGCACTAGGTCCAAATATTATGAACCCTACTCCTGCAACTGAATTGGCCGTCCAGGCGATATTTAAAAGGGGTTACTAGCAGGTTCATCGGAGCCCGTACTCCTTCCGGGCATAGTCGTTCGACGGGTAGAAATTCATCCAGTCGTGCCGGATACCCCGAGAATACCCCTATTTTTTGATCCTTCACCATCATCGTCCGCGGACTCATCTAAGTACCTCAGACCGAAACTGTTATCGTAGCGAAGAGCGAACTCGAATGACATCGCTTGTCCAACAGGGAAAATATGTAAAGTATATGCAGATTATTATAGGAAGATCACAAACTCCATCGCGCCTAGGCCAAAGACTTGCCAGAACAACATCTCTTCCAGAGCAAGGAAGTGTTTGAACCTCACTATTATCGAGAGAAGTCCCATGAATTTATAATAGTGAGGCTCAAAAACTTCCTTCATCGTCGGGCGCTGGGGCGAGCTAGGCTTCCCTAGCCGTCATTACTGTACCCACGCCAAATATTATCGAGTATGACTACGAAGCCTTCACAAGGGGCTGCAGTAACAGACTAACTCACGCACACCGCAACTACTATCCTAAATTGAGTAAGAAAACCCTCGACTATAGCCCTTGAATTAAATTCGCTATTTAAGGAAGACCGCGCTTCCGCTCGCCCGCGTATAGTTTACCAGTCCCAAGAAACATGTGTGGCCAGCCTACCTGAAGAGCTGTGTAAAGATAGATTCTGACATCCTCAAAAAGAAGTTTTCGAGCCGCACTACTACGCACGGAGCTCCGTTATTCAAGGCATGTCGAAGTACAGCGTGGTGGCCTCTCCTGTTCTCCACCCCAGCTAAACCCACCTTGTTCGAATTCGCGCAACTGTATATGACATGAACACTTACAGGGGTTAGAAGTTACTGCAACCAAGATAGAGCTCGTCGAAGTAATAGTGCGGTTCAAAAACTTCCTTCAATTGGTCTCATCACTTAAATTTAAGAGCTATTGTGAGTACAGGTACGGATGCGGCTTCAGTGGATCTTCAGCATTCATTCCTTGTAGTAATGGGGTTCGAAGACTTCCTTGCCAGGGTACCAAACAAGTCTTGCGCATCCTCCTCCCTAAGGAGGTATTTGAACCCCACTATTACCCACGATAGAACATGCAGGGTTTGATAGTGGAACACCTTTTAGAATCTGGGGATAAATTCCCAGGACTAATGTATGGCTGTAGTAATGAGGTTCAAAAACCTCCTTTTCAGGTGGATCGCAGGCCGTGCTGCCTCACAAGCTGGGACGCCGTCCACGGTATAGCCGGCGTCGGCAGTTACTGTGAAATAGCGGAAACTCGATCCCAATATATCATCTTACGTTTGGCGCCCAATAGTCGCCCAGTACCCGTTGACAGTTCTTTAACTCGGCTTAGAACTACTAGACAGGTTCAACCGAACCTTGCCCTAGTTCCCACTCCCGTAATTCATTTGGGTTTGCATCTAGAATACTGGAGGGTGCATAGACGAAACGTGTACGTCGGAGAAAACGTAAGAAAATGACCTAGACTCATAGTAGTGAGGTTCGAAGACTTCCTTTCAGTGAAATCGATCCACCACTCGCCGCGAAGAGATAATAGCATAGAGCACAAGTGCGCGAGTAGAGAAAAAGGCTATCCCAACCGGGCACGTCCTTCGTGTTTGGCGTTTACATACGGCACCCCGTTTCTGCACGTTAACCGTCTAGTATCCAACGGTGGATGGCGGACGCTAGACTATAGATATGAGATATCGAGACCTGGAGCTGGGTGTGGCTGCAGCCCGGGTCATTGCGGGCTGTGAATTCAAGGGCATGTAAACAAGCGTATATCGAACAGTGGATGGGCACCTGCAATACTCACGGTAGAGTTAGCTCACAGGATTCACGTTGAGGACTATGAGTCCCTCTTCGCTAGCAGTCTGGGGGGATATGGAGTTTAATAGCTTGACGTAGTAATGGGGCTCAAACACCTCTTTGTGTGAGCACAGCTACTTGCATTAAGAGATTCTAAACAGCGATCATCTCGGCTATTTCGGGCCAGCCTTTTCGGCAGGATGTTATGTAGCATTTCTGGAAGCTTCCCCCTCGAATCTACTAGTGGTGAGAAGATGCCCCACCGATATTACTCTTTAATCTTGAGAAACCTAAAACCGATCTGACCTCAGACGGGCGGCTCCCACCCGAGGATAAACTCGTCAATAATAATGTGGCTCGAACACTTCTTTTCTCACTAGGCTTTTACGACACGCCACATGTATTTAAGCATCTACCTAACTTGTGTCTGCTGATATACAGCGCATTCTACGCCCAACCTACCAATTACTTCAACGTAGTGCGTGGCTAAAATTCAGGGGAGCTTCATCTCTGTCTTAATTTGAAGGTTCTTCCGGGGCGTTTGGGAATCTTCGTGCCTTTTGCGAGGTTAAGGTATCAAAGAAGTTTTCGAACCACATTATTACCGCCTTAAGCACCGGCGCATCCTGCTCGTGACAACTCTACCCTGCCCTGATAAAGGCACTGAACGTTCCAGAGAGTGCATCATTGACACGCGAGCAGGCCACAGTAGCCACAAGACGTATGGGTGATTATAGAATTGGTGGAGGTGTTGTTAACGATCAGGAGGACATTAGTGGGAGTTAGGAAAGACCCTATGTTCTCTCTATCGCGGACTTGTAACTTGACAAGCAAAAGGGTAAGAGAGCTGCACACCGAAGCAGGCCCTTCCTATACCTGTTTTTCCTACGCGTAGAGAGGAATCCAGAAAGGTGATAATTGGCATTCGATGAAAAAACAGTGTGCCACTGACTTAGTTCTATATGTGAAGAGCCTGTTAGCACGTGACGGCGGCCTTGGTATAGAGCCCTTAATGGTCTCCATCGCGTAGTAATGGGGCTCGAAAACCTCCTTACTTGGGATTGCGTGGCCTCCTTGTGAGTCATACACAAGGCTTAGGGCTATGGGGCGATACACTCCTTTTCGCGGCGCATGGGGCGGTGATGCCTACATAGTAGTAGTGACTGCCTTTCTGGGGGGCTATTTGTGGATGACCAACACCTGACCAGCGATGCAATCGCTAGGGGAGGTACACCTCTCATATGTTACAACAATCACCGAATTGTGTTTCGAATTCGAATCAAGTTTGCGGTGTCGACCAGATCTGGTCTTGCTGCCATACCGGGTTCGCCGCCTCCGGTGGATAGAACTGCATCTTAAGACATCTGGACCCAGCGGTAAGTAGCGGGAAGAGTTTAGAGTCATTCGTACAACTACAGGCTAAGGGCTTACTGGGGAGTTGTTGTAGGGCATAAAGATCGCCCCATGACTTTTCGTACTTTCCCCGATAGTTCACTCGCAGCGAGCTGCGGCTGGGCTTCGCCACACGAGTACGGGCAACATTTATCTCCTCTAATCACTGGGCACCGCGCGAGGAAATAGAAAACCCTAATCAGTGCTCATGGGCGCATCTATTGGTCTCCGCATGCACGATGCCGCGGAGTGCTTAGTTGTCCCTGCATAATCTTCGTAGATGTATAAGAGATTACCTATTTATTCGGTTTCGGTTCTAGACGTACCTTGCCGCATGAGTATAGGCTAATGAACTGAGTTGGCGCCAGAGGGAAAGGCATAATAATGCGGCTCGAATACTTCCTTAAGGAAGTATTCGAACCACATTACTAT";
//The given peptide
string givenPeptide = "KEVFEPHYY";
int resultCount = 0;
int candidateLength = givenPeptide.Length * 3;
string forward = "";
string backward = "";
for (int pos = 0; pos < dna.Length - candidateLength + 1; pos++) // Added the "+ 1"
{
forward = dna.Substring(pos, candidateLength);
backward = DNAComplement(forward);
// Unremark to get a glimpse into what is happening...
//Console.WriteLine("pos: {0}, forward: {1}, backward: {2}, forwardTranslation: {3}, backwardTranslation: {4}",
// pos, forward, backward, StringToAminoAcid(forward), StringToAminoAcid(backward));
if (
(StringToAminoAcid(forward) == givenPeptide)
||
(StringToAminoAcid(backward) == givenPeptide)
)
{
resultCount++;
Console.WriteLine(String.Format("Result {0,3} at position {1,6}... {2}", resultCount, pos, forward));
}
}
Console.WriteLine("Done");
Console.ReadLine();
}//end main
}//end class
}