pex8_8.cpp
来自「数据结构C++代码,经典代码,受益多多,希望大家多多支持」· C++ 代码 · 共 69 行
CPP
69 行
#include <iostream.h>
#pragma hdrstop
#include "strclass.h"
// replace repstr.Length() chars in source starting at index pos
void Replace(String& source, int pos, const String& repstr)
{
// remove repstr.Length() chars from source beginning
// at index pos. if fewer than repstr.Length() chars
// exist in the tail of source, delete entire tail
source.Remove(pos, repstr.Length());
// insert repstr into source at index pos.
// if source.Length() = pos, append repstr to source
source.Insert(repstr, pos);
}
// print S centered in a line of 80 '%' characters
void Center (String& S)
{
String line;
int startS;
// create line = "%%%...%%%"
for(int i=0;i < 80;i++)
line += "%";
// compute starting index of S
startS = (80 - S.Length())/2;
// replace S.Length() '%' chars beginning at index startS
Replace(line,startS,S);
// output the line
cout << line << endl;
}
void main(void)
{
// testing strings
String S("SourceString"), T("123"), U;
U = S;
// replace at beginning of U
Replace(U,0,T);
cout << "1st example: " << U << endl;
// replace inside U
U = S;
Replace(U,6,T);
cout << "2nd example: " << U << endl;
// replacement lengthens U
U = S;
Replace(U,11,T);
cout << "3rd example: " << U << endl;
Center(S);
}
/*
<Run>
1st example: 123rceString
2nd example: Source123ing
3rd example: SourceStrin123
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%SourceString%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?