Hi
I am trying to reverse a string and then replace some of the characters . All I want is to replace the characters along the string. I used for loop for that purpose, but it does not work. For example if I have a string AATT I want to get TTAA, but intead I am getting AATT again because I am replacing first A with T and then again T with A.. Is there a way to solve this problem.
This is the code:
public DNAsequence revComp(){
DNAsequence reverseDNA = null;
StringBuilder sb = new StringBuilder(this.getContent());
sb.reverse();
String reverse = new String(sb);
for (int i=0;i<=reverse.length()-1; i++) {
if (reverse.charAt(i)=='A'){ reverse = reverse.replace('A','T');}
if (reverse.charAt(i)=='T') {reverse = reverse.replace('T', 'A');}
}
reverseDNA = new DNAsequence(reverse, this.getDescription());
return reverseDNA;
}
replace characters in a string
Page 1 of 17 Replies - 2604 Views - Last Post: 11 April 2007 - 06:06 AM
Replies To: replace characters in a string
#2
Re: replace characters in a string
Posted 10 April 2007 - 08:55 AM
Are you trying to replace? Or simply reverse?
#3
Re: replace characters in a string
Posted 10 April 2007 - 09:18 AM
I'm not seeing the problem, your output is exactly what you wanted, you reversed the string then replaced A -> T and T -> A.
Why not try a string that isn't all As and Ts to see if it works?
Why not try a string that isn't all As and Ts to see if it works?
#4
Re: replace characters in a string
Posted 10 April 2007 - 11:34 AM
William_Wilson, on 10 Apr, 2007 - 09:18 AM, said:
I'm not seeing the problem, your output is exactly what you wanted, you reversed the string then replaced A -> T and T -> A.
Why not try a string that isn't all As and Ts to see if it works?
Why not try a string that isn't all As and Ts to see if it works?
Hi,
Unfortunetly there is a problem. I want to replace ATA to TAT but I have got only TTT. I do not know how to replace each character one by one?? I tried the for loop but it does not work. Any help please?
#5
Re: replace characters in a string
Posted 10 April 2007 - 12:59 PM
create a char array, of the string would be easiest i think, i believe its:
char replace[] = charArray(sb);
but you may want to check the java doc to be sure. Then loop through the array checking each char. the way you have above, there should be a method to transform the array back into a string when you are finished.
char replace[] = charArray(sb);
but you may want to check the java doc to be sure. Then loop through the array checking each char. the way you have above, there should be a method to transform the array back into a string when you are finished.
#6
Re: replace characters in a string
Posted 11 April 2007 - 01:59 AM
Hi.
I think I get it why it isn't working.
You are doing a replace of one char and then of the other.
So if you have TAT and you want to get ATA then you have to be able to do the replace of all chars at the same time otherwise you will have the following.
You have TAT, replace the t with a, you get AAA, replace the a with t and you end with TTT.
The solution with moving the string to a char array, do the replace and move again to a string is the easiest to achieve this.
Come to think of it, I don't seem to know another way to get the result otherwise.
Damn, reading such things makes me want to stop doing functional work and go back to doing real programming again.
I think I get it why it isn't working.
You are doing a replace of one char and then of the other.
So if you have TAT and you want to get ATA then you have to be able to do the replace of all chars at the same time otherwise you will have the following.
You have TAT, replace the t with a, you get AAA, replace the a with t and you end with TTT.
The solution with moving the string to a char array, do the replace and move again to a string is the easiest to achieve this.
Come to think of it, I don't seem to know another way to get the result otherwise.
Damn, reading such things makes me want to stop doing functional work and go back to doing real programming again.
#7
Re: replace characters in a string
Posted 11 April 2007 - 04:35 AM
Another way to reverse a string (without using the built-in reverse method) is to push its members onto a stack in order and then pop them off.
pseudo-code:
String str = "AABC"
Stack stk = {} // empty
push "A" onto stk, stk is {"A"}
push "A" onto stk, stk is {"A", "A"}
push "B" onto stk, stk is {"B", "A", "A"}
push "C" onto stk, stk is {"C", "B", "A", "A"}
String newStr = ""
newStr += stk.pop(), newStr is "C"
newStr += stk.pop(), newStr is "CB"
newStr += stk.pop(), newStr is "CBA"
newStr += stk.pop(), newStr is "CBAA"
pseudo-code:
String str = "AABC"
Stack stk = {} // empty
push "A" onto stk, stk is {"A"}
push "A" onto stk, stk is {"A", "A"}
push "B" onto stk, stk is {"B", "A", "A"}
push "C" onto stk, stk is {"C", "B", "A", "A"}
String newStr = ""
newStr += stk.pop(), newStr is "C"
newStr += stk.pop(), newStr is "CB"
newStr += stk.pop(), newStr is "CBA"
newStr += stk.pop(), newStr is "CBAA"
#8
Re: replace characters in a string
Posted 11 April 2007 - 06:06 AM
Thanks to All,
I solved the problem with string buffer.
I solved the problem with string buffer.
alcdotcom, on 11 Apr, 2007 - 04:35 AM, said:
Another way to reverse a string (without using the built-in reverse method) is to push its members onto a stack in order and then pop them off.
pseudo-code:
String str = "AABC"
Stack stk = {} // empty
push "A" onto stk, stk is {"A"}
push "A" onto stk, stk is {"A", "A"}
push "B" onto stk, stk is {"B", "A", "A"}
push "C" onto stk, stk is {"C", "B", "A", "A"}
String newStr = ""
newStr += stk.pop(), newStr is "C"
newStr += stk.pop(), newStr is "CB"
newStr += stk.pop(), newStr is "CBA"
newStr += stk.pop(), newStr is "CBAA"
pseudo-code:
String str = "AABC"
Stack stk = {} // empty
push "A" onto stk, stk is {"A"}
push "A" onto stk, stk is {"A", "A"}
push "B" onto stk, stk is {"B", "A", "A"}
push "C" onto stk, stk is {"C", "B", "A", "A"}
String newStr = ""
newStr += stk.pop(), newStr is "C"
newStr += stk.pop(), newStr is "CB"
newStr += stk.pop(), newStr is "CBA"
newStr += stk.pop(), newStr is "CBAA"
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|