Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

how to stop index of loop

$
0
0
i want to count number of times "dog" appears on a sentence but my code its creating a loop

how can i stop this loop??


println(input);
    int index = -1;
    int count = 0;
    print("Counting dogs:");
    do{
       index = input.indexof("dog"); // the loop starts here
      if(index != -1){
        count++;
        print(count+" ");
      }
    }while(index != -1);
    println("The word \"dog\" appears "+count+" times.");
  } 
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>