2011-11-15 49 views
0

我有一個字符串,我需要從開始處繼續減去字母,並且字母連續移動。例如:在字符串的開始處減去

ABC DEF GHI JK需要像這樣 BCD EFG HIJ K和再 CDE FGH IJK

我有一些代碼,但字母不單獨移動:

int main() 
{ 
    string code, default_Code; 
    default_Code = "TCAATGTAACGCGCTACCCGGAGCTCTGGGCCCAAATTTCATCCACT";   
    start_C = "AUG"; 
    code.reserve(100); 
    int i = 0, a = 0, c =0; 
    char choice; 

    for (int j = 3; j < code.length(); j += 4) // Creating space between 3 letters 
    { 
     code.insert(j, 1, ' '); 
    } 

    do { 
     i = 0; 
     do {            // Looping to create an open reading frame. 
      for (int b = 0; b*3 < code.length(); b++) {   // moving through the code 
       for (int a = 0; a < 3; a++) { 
        cout << code[(a + b*3) + i]; 
       } 
      } 
      i++; 
      cout << endl; 
     } while (i < 3); 

     reverse(code.rbegin(), code.rend());   // Reversing to create the second set reading frame. 

     c++; 
     cout << endl; 

    } while (c < 2); 
    return 0; 
} 
+0

試圖清理格式化一下。 –

回答

0

如果你真想把空格字符到字符串,而不是僅僅把它們插入到輸出,你可以做這樣的事情:

std::string code("TCAATGTAACGCGCTACCCGGAGCTCTGGGCCCAAATTTCATCCACT"); 

// a repeating pattern of offsets 
int x[4] = { 1, 1, 2, 0 }; 

for (size_t i = 3; i < code.length(); i +=4) 
{ 
    code.insert(i, 1, ' '); 
} 

while (code.length() > 0) 
{ 
    printf("%s\n", code.c_str()); 

    for (size_t i = 0; i < code.length() - 1; i++) 
    { 
     // new char is either 1, 2, or 0 bytes ahead 
     size_t j = i + x[i % 4]; 

     // in case it's 2 bytes at the end, just get the last 
     code[i] = code[j < code.length() ? j : j - 1]; 
    } 

    // and throw away the leftover at the end 
    code.resize(code.length() - 1); 
} 

編輯>要到下面的意見作出迴應:

如果您嘗試插入printf語句之前這段代碼...

size_t t = code.find("ATC"); 
    if (t != std::string::npos) 
    { 
     code.replace(t, 3, " M "); 
    } 

..你會發現code.find(「ATC」)確實是而不是找到「A TC」或「AT C」。直到輸出的第三行,當「ATC」出現在字符串中時,在字母之間沒有任何空格時纔會找到它。我有空格插入實際的字符串數據,就像您的原始代碼一樣。

+0

謝謝,但似乎每次都會遇到同樣的問題。現在我應該尋找某個字母塊。讓我們從我的例子上面看看。假設我想查找DEF。當下一個塊轉到BCD EFG HIJ等時,不應該再有DEF了。但是,當我運行我的程序時似乎並非如此。 – user1046825

+0

即使在字母之間有空格時,它似乎也能找到DEF的所有實例。我將發佈我在下面使用的代碼來搜索並替換我正在尋找的代碼。 – user1046825

+0

size_t pos = code.find(「ATG」); if(pos!= string :: npos){code.replace(code.find(「ATG」),3,「M」);} – user1046825

0

不是從字符串中刪除第一個字符,只是遍歷字符串中的起始位置。

0

你應該看看: http://www.cplusplus.com/reference/string/string/substr/

從字符串對象中提取某一部分出。

將要打印的字符串留在一個變量中,並將格式化的輸出字符串留在另一個變量中。

元代碼:

的std :: string值= 「ABCDEFG」 的std :: string輸出;

value = value.substr(2,value.size() - 3);

//創建輸出 輸出=值。等等。

0

你的代碼,因爲你在code工作不打印任何東西了,但你從來沒有真正設置該變量任何東西,除了一個空串。我認爲這只是因爲你發佈的代碼是根據你實際使用的內容進行修改的。

我建議採取不同的方式來做到這一點。

編寫一個函數,該函數接受一個輸入字符串並返回一個插入了空格的數據。然後重複調用該函數,刪除初始字符。

這裏有一個程序,打印出你的字符串分成三個字母組,刪除每個打印行後的第一個字母,直到沒有打印任何東西。

void print_with_spaces(string::const_iterator begin,string::const_iterator end) { 
    int i=0; 
    while(begin!=end) { 
     cout << *begin++; 
     if(++i % 3 == 0) 
      cout << ' '; 
    } 
    cout << '\n'; 
} 

int main() { 
    string code = "TCAATGTAACGCGCTACCCGGAGCTCTGGGCCCAAATTTCATCCACT"; 

    string::const_iterator i = code.begin(); 
    while(i!=code.end()) 
     print_with_spaces(i++,code.end()); 
} 
+0

那麼如果我只想循環3次呢?我不能在那裏放置數值...... – user1046825

+0

而不是'while(i!= code.end())''你會有'for(int j = 0; j <3; ++ j)' – bames53

+0

謝謝,但我還有一個問題......當我嘗試閱讀該代碼中的一組字母時,即使它已分離,它仍會繼續閱讀它。例如,TCA ATG TAA CGC GCT ACC CGG AGC TCT GGG CCC AAA TTT CAT CCA CT CAA TGT AAC GCG CTA CCC GGA GCT CTG GGC CCA AAT TTC ATC CAC T對於第一行的第二個區段,有ATG,並且那麼第二行沒有ATG,但編譯器仍在讀取它。有什麼方法來改變布特<<''的功能嗎?實際上插入一個像插入fn一樣的空間。 – user1046825