Reputation: 1295
I have this program that is supposed to find a string of 3 chars, say "abc" into a longer string say "aabccbd" and then replace the coincidence with another char (say 'd') into the original string. So. Given those strings the results would be:
aabcabcd
adabcd <<-- another iteration
addd <<-- Final result
And I've written a few code lines that I believe solve the problem of replacing the given sequence (Still haven't programmed the displacing of chars). But the thing is that when I replace the sequence with the given char, the char that is before the sequence gets deleted. Example:
aabcabcd
dabcd <<-- Incorrect
What could possibly be wrong? Check my code please. The sequence is read from a .txt file with the following format:
a b c d
<- Sequence and swapping character. (Sequence is always 3 chars long)
abcdefakxoqp333
<- Complete string where I'm supposed to search for the sequence.
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;
}
I know I can try with regex, the thing is I'm kind of restricted on the use of C++11. I also know I can use string::replace but what's wrong with this code? Thank you!
Upvotes: 1
Views: 88
Reputation: 1857
This is not a comparison but assignment, replace =
with ==
if(dna[index]=gen[0]...
Upvotes: 2