Show your support by donating any amount. (Note: We are still technically a for-profit company, so your
contribution is not tax-deductible.)
PayPal Acct:
Feedback:
Donate to VoyForums (PayPal):
[ Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time ] |
#include
#include
void findstring(char*, char*, short*, short&);
void main()
{
char str[101];
char word[11];
short offset[101];
short Cnt;
short i;
cout << "Please input a string.\n";
cin.getline(str,101);
while(1)
{
cout << "Please input a word (enter to exit): ";
cin.getline(word,11);
if(strcmp("", word) == 0) break;
findstring(str,word,offset,Cnt);
if(Cnt > 0)
{
cout << "Occurred times : " << Cnt << endl;
cout << "Offsets : ";
for(i=0; i< Cnt; i++)
cout << offset[i] << " ";
cout << endl;
}
else
cout << "Couldn't find the substring!\n";
}
}
void findstring(char str[], char word[], short offset[], short& Cnt)
{
short i, j, str_len, word_len;
str_len = strlen(str);
word_len = strlen(word);
Cnt = 0;
for(i = 0 ; i < str_len ; i++)
{
if(str[i] == word[0])
{
for(j=1; j < word_len ; j++) //j from 1 to length of the word
{
if(str[i+j] != word[j])
break;
}
if(j == word_len)
offset[Cnt++] = i;
}
}
}
=========================================================