Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Find the count of a letter in a string

function letterCount(str, c) {
  var count=0;
  str=str.toLowerCase(); //Case insensitive
  for(var i=0;i<str.length;i++){
     if(str.charAt(i)==c){ 
       count++; //Increment count
     }
  }
  return count;
}

console.log(letterCount("Connect",'c')) // 2
console.log(letterCount("first person",'s')) // 2
Source by contactmentor.com #
 
PREVIOUS NEXT
Tagged: #Find #count #letter #string
ADD COMMENT
Topic
Name
6+5 =