Input: first string: "This is fishsky's Chinese site: http://www.fishsky.com.cn/cn"
Substring: "fishsky"
Output: "NC/nC. MOC. fishsky. www //: Ptth: ETIS esenihc s 'fishsky Si siht"
The general method is to first scan the first string on one side, then use stack to reverse it, and record the position where the substring appears. Scan the recorded sub-string again and use the stack to reverse it. The method I used is to scan the array once. If a substring is found in the scan, it is pushed to the stack.
Finally, the characters in the stack Are popped up, so that the strings are restored to their original order.
==========================================
# Include <iostream>
# Include <stack>
# Include <assert. h>
Using namespace STD;
Const char * reverse (const char * STR, const char * substr)
{
Assert (STR & substr );
Stack <char> stack1;
Const char * psubstr = substr, * head = STR, * rear = STR;
While (* head! = '/0 ')
{
While (* head! = '/0' & * psubstr = * head)
{
Psubstr ++;
Head ++;
}
If (* psubstr = '/0 ')
{
Const char * P;
For (P = head-1; P> = rear; p --)
Stack1.push (* P );
Psubstr = substr;
Rear = head;
}
Else
{
Stack1.push (* rear );
Head = ++ rear;
Psubstr = substr;
}
}
Char * return_str = new char [strlen (STR) + 1];
Int I = 0;
While (! Stack1.empty ())
{
Return_str [I ++] = stack1.top ();
Stack1.pop ();
}
Return_str [I] = '/0 ';
Return return_str;
}
Char * reverse1 (const char * STR, const char * substr)
{
Stack <char> stack1;
Int Len = strlen (STR );
Char * temp = new char [Len + 1];
Const char * psubstr = substr;
Strcpy (temp, STR );
// Traverse the string
For (INT I = 0; I <Len; I ++)
{
Int J = 0; // J indicates the subscript pointing to substr.
// If the first start loop is found
While (STR [I] = substr [J] & substr [J]! = '/0') // temp
{
J ++;
I ++;
}
// J exceeds the length of substr, indicating that
If (j = strlen (substr ))
{
J --; // substr subscript minus 1
For (; j> = 0; j --)
Stack1.push (substr [J]); // temp
I --;
}
Else
Stack1.push (STR [I]); // temp
}
Int K = 0;
While (! Stack1.empty ())
{
Temp [k ++] = stack1.top ();
Stack1.pop ();
}
Temp [k] = '/0 ';
Return temp;
}
Int main (INT argc, char * argv [])
{
Char * STR = "this is fishsky's Chinese site: http://www.fishsky.com.cn/cn ";
Cout <STR <Endl;
// Cout <reverse (STR, "fishsky") <Endl;
Cout <reverse1 (STR, substr) <Endl;
Return 0;
}
(2) database functions
# Include <iostream>
# Include <string. h>
Using namespace STD;
Void strdeal (char * STR, char * sunbtr)
{
Char * Start, * end, * PTR;
PTR = Start = STR;
While (* PTR ++)
;
End = ptr-2;
While (start <End)
Swap (* Start ++, * end --);
Cout <STR <Endl;
Char * temp = strrev (strdup (sunbtr ));
PTR = STR;
While (* PTR)
{
Start = strstr (PTR, temp );
If (! Start)
Break;
End = start + strlen (sunbtr)-1;
PTR = start + strlen (sunbtr );
While (start <End)
Swap (* Start ++, * end --);
}
Cout <STR <Endl;
}
Int main ()
{
Char * STR = "this is fishsky's Chinese site: http://www.fishsky.com.cn/cn ";
Strdeal (STR, "fishsky ");
Return 0;
}