OneCompiler

Count string's space using js

143

let str="how are u looking today";
let space=0;
//let x = str.charAt();
for (let i=0; i<=str.length;i++)
if(str.charAt(i)==" "){
space += 1;

}
console.log("count space:"+space);

let str1= "hello world";
let count= 0;
for (let i=0;i<str1.length;i++){
let ch = str1.charAt(i);
if(ch==" "){
count += 1;
}
}
console.log("count space:"+count);