Write a function clean(dna) that returns a new DNA string in which every character that is not an A, C, G, or T is replaced with an N. For example, clean('goat') should return the string 'gnat'.
You can assume dna is all lowercase, but don't assume anything about the nature of the wrong characters (e.g. they could even have been accidentally transcribed as numbers).
No importing
Must use for loop
package dna;
import java.util.Scanner;
public class DNA {
private static String clean(String DNA) {
StringBuilder sb = new StringBuilder();//function to replace characters with n
for (int i = 0; i < DNA.length(); i++) {
if (DNA.charAt(i) == 'a' || DNA.charAt(i) == 'c' || DNA.charAt(i) == 'g' || DNA.charAt(i) == 't') {
sb.append(DNA.charAt(i));
} else {
sb.append("n");
}
}
return sb.toString();
}
public static void main(String[] args) { //main function
Scanner sc = new Scanner(System.in);
String dna = sc.nextLine();
System.out.println("Clean DNA: " + clean(dna));
}
}
Output:-
Note:- Please comment if you face any problem. :)
Get Answers For Free
Most questions answered within 1 hours.