我的程序正在从字符串中删除一个字符,如何避免这种情况
My program is deleting a character from a string, how to avoid that?
我有这个程序,它应该找到一个3个字符的字符串,把"abc"说成一个更长的字符串,说"aabccbd",然后用另一个字符(说'd')替换成原始字符串的巧合。所以。给定这些字符串,结果将是:
-
aabcabcd
-
adabcd <<-- another iteration
-
addd <<-- Final result
和我已经写了一些代码行,我相信解决了替换给定序列的问题(仍然没有编程替换字符)。但问题是,当我用给定的字符替换序列时,在序列被删除之前的字符。例子:
-
aabcabcd
-
dabcd <<-- Incorrect
可能出什么事了?请检查我的代码。序列从.txt文件中读取,格式如下:
a b c d
<-序列和交换字符。(序列总是3个字符长)abcdefakxoqp333
<-我应该在其中搜索序列的完整字符串。
Code:
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
char* gen = new char[3];
char simp;
char dna[10001];
int main() {
int len;
//Reading file and assigning values.
FILE *input = fopen("input.txt","rt");
fscanf(input,"%c %c %c %c",&gen[0],&gen[1],&gen[2],&simp);
fscanf(input,"%s",&dna);
fclose(input);
len = strlen(dna);
//In this part dna has the 2nd line of input.txt
for(int i=0;i<len;i++){
cout << dna[i];
}
cout << endl;
//Searching for coincidences.
int dnaLen = len; //dna Lenght, this one will change over time.
bool found=false;
int index=0; //Index will be reseted as well when we make a change!!
while(index<dnaLen-2) {
cout << "Entering WHILE with index=" << index << "n";
cout << "Examining: " << dna[index] << dna[index+1] << dna[index+2] << endl;
if(dna[index]=gen[0] && dna[index+1]==gen[1] && dna[index+2]==gen[2]){
//Replace dna[index] for simp AND displace the dna array
dna[index]=simp;
for(int i=0;i<dnaLen;i++){
cout << dna[i];
}
cout << endl;
index =0;
dnaLen=dnaLen-2;
}
index++;
}
return 0;
}
我知道我可以尝试使用正则表达式,问题是我在c++ 11的使用上受到了限制。我也知道我可以使用string::replace,但这段代码有什么问题?谢谢你!
这不是比较,而是赋值,将=
替换为==
if(dna[index]=gen[0]...
相关文章:
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何检查一个c++字符串中有多少相同的字符/数字
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 关于字符数组,我正在尝试将数组中的每个字符分配给另一个值
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用动态分配将 char* 复制到另一个字符**
- 将字符向量复制到另一个向量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 如果我们在其中输入一个整数,则字符会给出整数作为输出,但是当分配给它一个整数时,这不会发生。为什么?
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- C ++读取用户输入的字符一个一个符号
- 遍历数组或从打开的文件中获取字符 - 一个比另一个有什么优势
- 将文本文件的内容一个字符一个字符地读入矢量,不跳过空白或新行
- 这是更快和更有效的,处理一个字符一个字符作为字符或流