7 Replies - 2894 Views - Last Post: 11 April 2007 - 06:06 AM Rate Topic: -----

#1 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

replace characters in a string

Posted 10 April 2007 - 08:46 AM

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;
}

Is This A Good Question/Topic? 0
  • +

Replies To: replace characters in a string

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: replace characters in a string

Posted 10 April 2007 - 08:55 AM

Are you trying to replace? Or simply reverse?
Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

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?
Was This Post Helpful? 0
  • +
  • -

#4 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: replace characters in a string

Posted 10 April 2007 - 11:34 AM

View PostWilliam_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?



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?
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

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.
Was This Post Helpful? 0
  • +
  • -

#6 AngeluS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 231
  • Joined: 11-April 07

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. :)
Was This Post Helpful? 0
  • +
  • -

#7 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

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"
Was This Post Helpful? 0
  • +
  • -

#8 stanka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-April 07

Re: replace characters in a string

Posted 11 April 2007 - 06:06 AM

Thanks to All,
I solved the problem with string buffer.


View Postalcdotcom, 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"

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1