// TestString.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "string"
#include "stack"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char *LongS="you are My love";
char *Find="My";
stack<char> MyStack;
char *phead=LongS;
char *rear=phead;
char *p=Find;
while(*phead!='/0')
{
while(*phead==*p)
{
phead++;
p++;
}
if(*p=='/0')
{
for(char *temp=phead-1;temp>=rear;temp--)
MyStack.push(*temp);
rear=phead;
p=Find;
}
else
{
MyStack.push(*rear++);
phead=rear;
p=Find;
}
}
while(!MyStack.empty())
{
cout<<MyStack.top();
MyStack.pop();
}
cin.get();
return 0;
}
本文通过一个具体的C++示例程序介绍了如何使用栈来实现字符串匹配功能。该程序定义了一个入口点,通过查找子字符串并在栈中逆序存储匹配部分,最终输出逆序后的结果。本文涉及C++编程语言的基本概念,包括指针操作、字符串处理及栈的使用。

被折叠的 条评论
为什么被折叠?



