📄 pex8_8.cpp
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -