1

Hi biologist here with a little bit of coding background. my goal is to be able to input a string of characters and the code to be able to tell me how many times they occur and at what location in the string. so ill be entering a string and i want the location and abundance of sq and tq within the string. with the location being the first character e.g njnsqjjfl sq would be located at postition 4.

This is what ive come up with so far (probably very wrong)

string S = "...";
int counter =0;
for(int i=0; i<s.length; i++){
if(s.charAt (i) == 'sq')}
counter++;})

string S = "...";
int counter =0;
for(int i=0; i<s.length; i++){
if(s.charAt (i) == 'tq')}
counter++;})

any input will help, thankyou

potame
  • 7,597
  • 4
  • 26
  • 33
  • Possible duplicate of [How to find nth occurrence of character in a string?](http://stackoverflow.com/questions/3976616/how-to-find-nth-occurrence-of-character-in-a-string) – Gideon Jun 09 '16 at 05:37

4 Answers4

1

So , you can have multiple occurrences of "sq" and "tq" in your code, so you can have 2 arraylists to save these two separately(or one to save them together).

ArrayList<Integer>sqLocation = new ArrayList<>();
ArrayList<Integer>tqLocation = new ArrayList<>();
for(int i =0;i<s.length()-1;i++){
if(s.charAt(i)=='s' && s.charAt(i+1)=='q'){
  sqLocation.add(i);
  }
else if(s.charAt(i)=='t' && s.charAt(i+1)=='q'){
  tqLocation.add(i);
  }
}
System.out.println("No. of times sq occurs = "+sqLocation.size());
System.out.println("Locations ="+sqLocation);
System.out.println("No. of times tq occurs = "+tqLocation.size());
System.out.println("Locations ="+tqLocation);
yash sachdeva
  • 637
  • 5
  • 13
0

This can be achieved using regex. Your use case is to count occurrences and position of those occurrences. The method match returns an integer list which is position and count is size of list

Exmaple code

public class RegexTest {
    public static List<Integer> match(String text, String regex) {
        List<Integer> matchedPos = new ArrayList<>();
        Matcher m = Pattern.compile("(?=(" + regex + "))").matcher(text);
        while(m.find()) {
            matchedPos.add(m.start());
        }

        return matchedPos;
    }

    public static void main(String[] args) {
        System.out.println(match("sadfsagsqltrtwrttqsqsqsqsqsqs", "sq"));
        System.out.println(match("sadfsagsqltrtwrttqksdfngjfngjntqtqtqtqtqtq", "tq"));
    }
}
Sangram Jadhav
  • 2,438
  • 16
  • 17
0

what you want is a HashMap <String, List <Integer>>

this will hold, the String that you are looking for e.g. sq or tq, and a List of the positions that they are at.

You want to loop around using String.indexOf see https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int)

psuedocode being

String contents = "sadfsagsqltrtwrttqksdfngjfngjntqtqtqtqtqtq";

map.add (lookFor, new ArrayList ());  

int index = 0;
while ((index = contents.indexOf (lookFor, index)) != -1) {

  list = map.get (lookFor);
  list.add (index);
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-1

You should use not charAt but substring to get a part of String.

int count(String s, String target) {
    int counter = 0;
    int tlen = target.length();
    for (int i = tlen; i < s.length(); i++) {
        if (s.substring(i - tlen, i).equals(target)) {
            counter++;
        }
    }
    return counter;
}

// in some method
count("...", "sq");
count("...", "tq");
MikeCAT
  • 73,922
  • 11
  • 45
  • 70