I wanna write a program, which gets a text at one line (maximum of text lenntgh is 20) and then user enters "end"
after that I ask user number of searchs and ask for words and then i say how many time any words repeated
it is an example output and input:
input:
It’s an example passage.
end
2
it
exam
Output
it : 1
exam : 1
this is my code:
#include <stdio.h>
int mystrlen(char *s)
{
int length = 0;
while (*s != '\0')
{
length++;
s++;
}
return length;
}
void match(char *haystack, char* needle, int d,int ns)
{
int j = 0;
int i, counter = 0;
for (i = 0; i < mystrlen(haystack); i++)
{
if (haystack[i] == needle[j])
{
if (j == mystrlen(needle) - 1)
{
counter++;
j = 0;
continue;
}
}
else
{
j = 0;
continue;
}
j++;
}
if (d < (ns-1))
{
printf("%s : %d\n", needle, counter);
}
else
{
printf("%s : %d", needle, counter);
}
}
int main()
{
int i = 0;
int ro;
char p;
char lol[10];
int ns;
char c_to_search[10][10];
char text[25];
gets(text);
i = mystrlen(text);
scanf("%s", &text[i]);
i = mystrlen(text);
text[i - 1] = '\0';
text[i - 2] = '\0';
text[i - 3] = '\0';
text[i - 4] = '\0';
for (i = 0; text[i] != '\0'; ++i)
{
if ((text[i]>64) && (text[i]<91))
text[i] = text[i] + 32;
}
scanf("%d", &ns);
i = 0;
scanf("%c", &p);
for (i = 0; i<ns; i++)
{
gets(c_to_search[i]);
}
for (i = 0; i<ns; ++i)
{
for (ro = 0; ro<mystrlen(c_to_search[i]); ++ro)
{
if ((c_to_search[i][ro]>64) && (c_to_search[i][ro]<91))
c_to_search[i][ro] = c_to_search[i][ro] + 32;
}
}
i = 0;
for (i; i<ns; ++i)
{
match(text, c_to_search[i],i,ns);
}
}
It works well but my problem is that sometimes the array index ranges are forgot and it causes Run time Error
Would you mind helping me how to fix that?
Comments
Post a Comment