Return the number of times the string "hello/Hello/...etc" appears anywhere in the given string.
The different in the problem is that
The string hello can be in any case i.e either upper case or lower case.
Sample Input #1
count("abc hello def")
Sample Output #1
1
Sample Input #2
count("Hi. Hello. Hello. Ok")
Sample Output #2
2
Sample Input #3
count("hi")
Sample Output #3
0
MyApproach
public int count(String str)
{
String str1="Hello";
int l=str.length();
int l1=str1.length();
if(l<l1)
{
return 0;
}
else
{
int count=0;
int p=0;
int j=0;
while(j<l)
{
char c=str.charAt(j);
char c1=str1.charAt(p);
if(c==c1)
{
p++;
if(p==l1)
{
count++;
p=0;
}
}
else
{
p=0;
}
j++;
}
return count;
}
}
Output TestcaseParameters Testcase Actual Answer Expected
No output 'HELLO how are you' 0 1
I am getting the following output.
Can anyone tell me what I am doing wrong?