1

How I do search for the string CTG from a file that is read as a string? And then give a count of how many times it shows up? For example, how would I add code to do this in here or anywhere in general:

public String readStrFromFile(){

    FileResource readFile = new FileResource();

    String DNA = readFile.asString();

    //System.out.println("DNA: " + DNA);

    return DNA;

}//end readStrFromFile() method;
A. K.
  • 81
  • 1
  • 4
  • 14

1 Answers1

1

You can use Regular expression

String DNA = readFile.asString();
Pattern pattern = Pattern.compile("CTG");
Matcher matcher = pattern.matcher(DNA);
while(matcher.find()){
    System.out.println(matcher.group() + " at " + matcher.start());
}

Or if the file is so big, you should use KMP algorithm or similar.

Edit :
You can have a counter for it.

int count = 0; 
while(matcher.find()){
    count++;
    System.out.println(matcher.group() + " at " + matcher.start());
}
System.out.println("Number of count : " + count);
afzalex
  • 8,598
  • 2
  • 34
  • 61