[ Next Thread |
Previous Thread |
Next Message |
Previous Message ]
Date Posted: 22:59:32 12/05/01 Wed
Author: Richard In Texas
Subject:
Substring searching...
³Ì¨S¦³®Ä²vªº¦r¦ê¤ñ¹ïµ{¦¡¡A¤£¹L¥i¥H°Ê§@¡C
§Ú²q³oµÛ¥i¯à·|¦Ò¡A§AÌ»{¬°©O¡H....
Run¬Ý¬Ý´Nª¾¹D¤F.....
============================================
#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;
}
}
}
=========================================================
[
Next Thread |
Previous Thread |
Next Message |
Previous Message
]