Java > String-2 > countCode (CodingBat Solution)

Problem:

Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2


Solution:

public int countCode(String str) {
  int len = str.length();
  int count = 0;
  String co = "co";
  String e = "e";
  
  if (len < 4)
    return 0;
  
  for (int i = 0; i < len - 3; i++) {
    if (co.compareTo(str.substring(i,i+2)) == 0 && e.compareTo(str.substring(i+3, i+4)) == 0)
      count++;
  }
  return count;
}


25 comments :

  1. public int countCode(String str)
    {
    int sum = 0;
    for (int i = 0; i < str.length()-3; ++i)
    {
    if (str.substring(i,i+2).equals("co") && str.charAt(i+3) == 'e') sum++;
    }
    return sum;
    }

    ReplyDelete
  2. Or using recursion...

    public int countCode(String str) {

    if (str.length() < 4)

    return 0;

    else if (str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e')

    return 1 + countCode(str.substring(4));

    else

    return countCode(str.substring(1));
    }

    ReplyDelete
  3. public int countCode(String str) {
    int c=0;
    for (int i=0; i < str.length()-3; i++)
    {
    if((str.substring(i,i+4).equals("code"))||str.substring(i,i+4).equals("cole")||str.substring(i,i+4).equals("cope")||str.substring(i,i+4).equals("coze")||str.substring(i,i+4).equals("core"))
    {
    c = ++c;
    }
    }
    return c;

    }

    ReplyDelete
  4. public int countCode(String str) {
    int result = 0;
    for (int i = 0; i <= str.length()-4 ; i++) {
    if (str.substring(i,i+4).matches("co[a-z]e")) result++;
    }
    return result;
    }

    ReplyDelete
    Replies
    1. to take this a step more you can use \\w instead of [a-z]

      Delete
  5. public int countCode(String str) {
    int count = 0;
    for (int i = 0; i < str.length()-3; i++){
    if (str.substring(i).startsWith("co") && str.charAt(i+3) == 'e')
    count++;
    }
    return count;
    }

    ReplyDelete
  6. Recursion :

    public int countCode(String str) {

    if(str.length()<=3)
    return 0;

    if(str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e' )
    return 1+countCode(str.substring(4));
    return 0 + countCode(str.substring(1));

    }

    ReplyDelete
    Replies
    1. public int countCode(String str) {

      if(str.length()<=3)
      return 0;

      if(str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e' )
      return 1+countCode(str.substring(4));
      return 0 + countCode(str.substring(1));

      }

      Delete
  7. public int countCode(String str) {
    int countCode = 0;

    for (int i = 0; i < str.length() - 3; i++)
    {
    if (str.substring(i, i + 2).equals("co") && str.substring(i + 3, i + 4).equals("e"))
    {
    countCode++;
    }

    }

    return countCode;
    }

    ReplyDelete

  8. int count = 0;

    for (int i=0; i<str.length()-3; i++) {
    if (str.substring(i, i+2).equals("co") && str.charAt(i+3) == 'e') {
    count++;
    }
    }
    return count;

    ReplyDelete
  9. public int countCode(String str) {
    int count = 0;
    for (int i = 0; i < str.length() - 3; i++) {
    if (str.substring(i,i+2).equals("co") && str.charAt(i+3) == 'e') {
    count++;
    i += 3;
    }
    }
    return count;
    }

    ReplyDelete
  10. public int countCode(String str) {
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
    if (str.substring(i).startsWith("co") && str.substring(i+3).startsWith("e")) {
    count++;
    }
    }
    return count;
    }

    ReplyDelete
  11. private final int countCode(String word) {
    if(word.length() <= 3){
    return 0;
    }
    if(word.charAt(0) == 'c'
    && word.charAt(1) == 'o'
    && word.charAt(3) == 'e'){
    return 1 + countCode(word.substring(4));
    }

    return countCode(word.substring(1));
    }

    ReplyDelete
  12. public int countCode(String str) {
    if(str.length() <= 3){
    return 0;
    }
    if(str.charAt(0) == 'c'
    && str.charAt(1) == 'o'
    && str.charAt(3) == 'e'){
    return 1 + countCode(str.substring(4));
    }
    return countCode(str.substring(1));
    }

    ReplyDelete
  13. public int countCode(String str) {
    if(str.length() <= 3){
    return 0;
    }
    return (str.charAt(0) == 'c'
    && str.charAt(1) == 'o'
    && str.charAt(3) == 'e')? (1 + countCode(str.substring(4))):countCode(str.substring(1));
    }

    ReplyDelete
  14. public int countCode(String str) {
    if(str.length() <= 3){
    return 0;
    }
    return (str.charAt(0) == 'c'
    && str.charAt(1) == 'o'
    && str.charAt(3) == 'e')?
    (1 + countCode(str.substring(4))):
    countCode(str.substring(1));
    }

    ReplyDelete
  15. public int countCode(String str) {
    return (str.length() <= 3)? 0:
    (str.charAt(0) == 'c' && str.charAt(1) == 'o' && str.charAt(3) == 'e')?
    1 + countCode(str.substring(4)) : countCode(str.substring(1));
    }
    }

    ReplyDelete
  16. public int countCode(String str) {
    return (str.length() <= 3)? 0:
    (str.charAt(0) == 'c' && str.charAt(1) == 'o' && str.charAt(3) == 'e')?
    1 + countCode(str.substring(4)) : countCode(str.substring(1));
    }

    ReplyDelete
  17. public int countCode(String str) {
    int count=0;
    for(int i=0;i<str.length()-3;i++){
    if((str.substring(i,i+4).equals("code"))||((str.substring(i,i+2).equals("co"))&&str.substring(i+3,i+4).equals("e")))
    count++;
    }
    return count;
    }

    ReplyDelete
  18. public int countCode(String str) {
    int count=0;
    for(int i=0;i<str.length()-3;i++){
    if(str.substring(i,i+4).matches("co[a-z]e"))
    count++;
    }
    return count;
    }

    ReplyDelete
  19. public int countCode(String str) {
    int count=0;
    for(int i=0;i<str.length()-3;i++){
    if((str.substring(i,i+2).equals("co"))&&(str.substring(i+3,i+4).equals("e"))){
    count++;
    }
    }return count;
    }

    ReplyDelete
  20. public int countCode(String str)
    {
    int count=0;
    if(str.length()<2)
    {
    return count ;
    }
    else
    {
    for(int i=0;i<=str.length()-4;i++)
    {
    if(str.charAt(i)=='c' && str.charAt(i+1)=='o' && str.charAt(i+3)=='e')
    {
    count++;
    }
    else
    {
    count=count;
    }
    }
    }
    return count;

    }

    ReplyDelete
  21. int cont = 0;
    for (int i=0; i<str.length()-3;i++){
    if (str.substring(i,i+2).equals("co") && (str.substring(i+3,i+4).equals("e"))){
    cont ++;
    }
    }
    return cont;

    ReplyDelete
  22. Another way

    public int countCode(String str) {
    return str.split("co\\we", - 1).length - 1;
    }

    ReplyDelete
  23. public int countCode(String str) {
    int code=0;
    for(int i=0; i<str.length()-3;i++){
    if(str.charAt(i)=='c'&&str.charAt(i+1)=='o'&&str.charAt(i+3)=='e')
    code++;
    }return code;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact