#include<stdio.h>
int main()
{
char a[]=”hello hellothethe hello the hello the hello”;
char b[]=”the”;
int c=0,i,j=0;
for(j=0;a[j]!=”;j++)
{
/* if(a[j]==’\t’||a[j]==’\n’||a[j]==’ ‘)
{
printf(“space\n”);
continue;
}
// else
*/ // {
i=0;
while(b[i]!=”)
{
if(b[i]!=a[j])
break;
i++;j++;
}
if(b[i]==”)
{
c++;
printf(“%d”,c);
}
else
{
while(a[j]!=”&& a[j]!=’ ‘) //it is working fine but there are double comparisons than in the next loop…
// while(a[j]!=’ ‘|| a[j]!=”) //why it not check the null condition…??? // while doing this case there is showing Garbage value at the end. because it is fail to check the character at the end of the string (which it should)… but there is half comparisons… so less execution time and complexity…
{
printf(“%c “,a[j]);
j++;
}
printf(“\n”);
}
// }
}
printf(“matched %d times\n”,c);
return 0;
}
null compair error is due to wrong compairing if you want to compare you have to mention something.
in above program you making a[j]!=” which is incorrect
a[j]!=’ ‘; that correct comparison of a[j] with a space.