cin.getline(t1,100,'\n');
change(s,t,t1);
cout<<"替换后的新文本为:
"<
system("pause");
}
六、运行结果
运行后界面如图,输入如图所示字符串
我们将其中的like替换成love操作如下
由图所示替换成功。
方案二、用VC++设计部分
一、界面设计
我们采用了当前最流行的VC++6.0作为开发工具,运行平台是WINDOW2000/XP。
通过C++提供的MFC类库,来设计更为简洁直观的对话框来实现字符串的查找与替换
操作界面流程图
点击运行查找替换字符串的可执行文件
在原始字符串中输入一段英语文章
在查找的字符串中输入要查找的字符串如图
点击查找按钮
如图所示会显示字符串的位置
在替换字符串文本栏输入要替换的字符串如图
点击替换按钮后如图所示
点击确定后文本框显示如下图所示
成功完成查找替换的操作
二、各个部分程序代码:
第一部分:
//SearchReplaceStr.cpp:
Definestheclassbehaviorsfortheapplication.
//
#include"stdafx.h"
#include"SearchReplaceStr.h"
#include"SearchReplaceStrDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CSearchReplaceStrApp
BEGIN_MESSAGE_MAP(CSearchReplaceStrApp,CWinApp)
//{{AFX_MSG_MAP(CSearchReplaceStrApp)
//NOTE-theClassWizardwilladdandremovemappingmacroshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
ON_COMMAND(ID_HELP,CWinApp:
:
OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CSearchReplaceStrAppconstruction
CSearchReplaceStrApp:
:
CSearchReplaceStrApp()
{
//TODO:
addconstructioncodehere,
//PlaceallsignificantinitializationinInitInstance
}
/////////////////////////////////////////////////////////////////////////////
//TheoneandonlyCSearchReplaceStrAppobject
CSearchReplaceStrApptheApp;
/////////////////////////////////////////////////////////////////////////////
//CSearchReplaceStrAppinitialization
BOOLCSearchReplaceStrApp:
:
InitInstance()
{
AfxEnableControlContainer();
//Standardinitialization
//Ifyouarenotusingthesefeaturesandwishtoreducethesize
//ofyourfinalexecutable,youshouldremovefromthefollowing
//thespecificinitializationroutinesyoudonotneed.
#ifdef_AFXDLL
Enable3dControls();//CallthiswhenusingMFCinasharedDLL
#else
Enable3dControlsStatic();//CallthiswhenlinkingtoMFCstatically
#endif
CSearchReplaceStrDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithOK
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
//dismissedwithCancel
}
//Sincethedialoghasbeenclosed,returnFALSEsothatweexitthe
//application,ratherthanstarttheapplication'smessagepump.
returnFALSE;
}
第二部分:
//SearchReplaceStrDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"SearchReplaceStr.h"
#include"SearchReplaceStrDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CSearchReplaceStrDlgdialog
CSearchReplaceStrDlg:
:
CSearchReplaceStrDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CSearchReplaceStrDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CSearchReplaceStrDlg)
m_strData=_T("");
m_strFind=_T("");
m_strReplace=_T("");
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCSearchReplaceStrDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSearchReplaceStrDlg)
DDX_Text(pDX,IDC_EDIT1,m_strData);
DDX_Text(pDX,IDC_EDIT2,m_strFind);
DDX_Text(pDX,IDC_EDIT3,m_strReplace);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSearchReplaceStrDlg,CDialog)
//{{AFX_MSG_MAP(CSearchReplaceStrDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_REPLACE,OnReplace)
ON_BN_CLICKED(IDC_SEARCH,OnSearch)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CSearchReplaceStrDlgmessagehandlers
BOOLCSearchReplaceStrDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCSearchReplaceStrDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCSearchReplaceStrDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCSearchReplaceStrDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCSearchReplaceStrDlg:
:
OnReplace()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
程序源代码
UpdateData(true);
if(m_strFind.IsEmpty())
{
AfxMessageBox("查找的字符串为空!
");//判断编辑框字符串是否为空
return;
}
intnum;
num=m_strData.Replace(m_strFind,m_strReplace);//替换字符串
CStringstr;
str.Format("共完成了%d处替换",num);
AfxMessageB