字符串操作 Beta 1.1
简介
经过两年的 C++ 编程学习,我初步编写了这款「字符串操作」程序。该程序有以下功能:
-
合并两个字符串
-
精确查找字符串
-
模糊查找字符串
-
截取字符串
-
替换一部分的字符串
-
查看两个字符串
特点:
- 使用 C++ 原生编译而成
- 简洁易上手
- 可修改性强
源码
#include <cstdio>
#include <conio.h>
#include <cstring>
#include <iostream>
using namespace std;
int FuzzyFindTimes(const string str1, const string str2, const int iCount)
{
int len1 = str1.length(), len2 = str2.length(), num = 0;
if (iCount == 1)
cout << "---------------第一个字符串---------------" << endl;
else cout << "---------------第二个字符串---------------" << endl;
for (size_t i = 0; i < len1; i++)
{
size_t pos;
if ((pos = str2.find_first_of(str1[i])) != string::npos)
{
num ++;
cout << "第" << num << "次:" << str2 << " 的第" << pos + 1
<< "个字符出现在 " << str1 << " 的第" << i + 1 << "个字符" << endl;
}
}
return num;
}
int find_number_of_times_of(const string str,
const string sub_str,
const int iCount)
{
int num = 0, pos = 0;
string::size_type i = 1;
if (iCount == 1)
cout << "----------第一个字符串----------" << endl;
else cout << "----------第二个字符串----------" << endl;
while ((pos = str.find(sub_str, pos)) != string::npos)
{
cout << "第" << i << "次:" << "第" << pos + 1 << "个字符" << endl;
pos ++;
num ++;
i ++;
}
return num;
}
int main()
{
/* The two of the following line are the most important variable */
string str1, str2;
/* The two of the above are the most important variable */
system("cls");
system("color 0f");
cout << "请依次输入两个字符串,以回车键为分隔:" << endl;
getline(cin, str1);
getline(cin, str2);
int len1 = str1.length(), len2 = str2.length();
cout << endl << "输入完成!" << endl;
getch();
string strChoose = "-1";
while (strChoose != "0")
{
system("cls");
cout << "╔═════════════════════╗" << endl
<< "║ 字符串操作" << endl
<< "║ 1. 合并两个字符串" << endl
<< "║ 2. 精确查找字符串" << endl
<< "║ 3. 模糊查找字符串" << endl
<< "║ 4. 截取字符串" << endl
<< "║ 5. 替换一部分的字符串" << endl
<< "║ 6. 查看两个字符串" << endl
<< "║ 7. 查看更新内容" << endl
<< "║ 0. 退出程序" << endl
<< "╚═════════════════════╝" << endl;
cout << endl << "请选择:" << endl;
cin >> strChoose;
getchar(); // 删除此行会导致功能1、2无法输入
// 合并字符串
while (strChoose == "1")
{
system("cls");
cout << " 合并后的字符串为:" << endl;
cout << str1 + str2 << endl;
getch();
break;
}
// 精确查找字符串
while (strChoose == "2")
{
system("cls");
cout << "您选择了进行精确查找的操作" << endl;
cout << "请输入你要查找的字符或字符串:" << endl;
string strExactlyFind;
getline(cin, strExactlyFind);
cout << endl;
int times1 = find_number_of_times_of(str1, strExactlyFind, 1);
if (times1 == 0)
cout << "未能在"" << str1 << ""中找到 "
<< strExactlyFind << endl;
else
cout << strExactlyFind << " 在 "" << str1 << ""里一共出现了 "
<< times1 << " 次" << endl;
cout << endl;
int times2 = find_number_of_times_of(str2, strExactlyFind, 2);
if (times2 == 0)
cout << "未能在"" << str2 << ""中找到 " << strExactlyFind << endl;
else
cout << strExactlyFind << " 在 "" << str2 << ""里一共出现了 "
<< times2 << " 次" << endl;
cout << endl;
getch();
break;
}/*End while*/
// 模糊查找字符串
while (strChoose == "3")
{
system("cls");
cout << "您选择了进行模糊查找的操作" << endl;
cout << "请输入你要查找的字符或字符串:" << endl;
string strFuzzyFind;
getline(cin, strFuzzyFind);
cout << endl;
int times1 = FuzzyFindTimes(str1, strFuzzyFind, 1);
if (times1 == 0)
cout << "未能在"" << str1 << ""中找到"" << strFuzzyFind
<< ""的任何一个字符" << endl;
else
cout << strFuzzyFind << " 在"" << str1 << ""里一共出现了" << times1
<< "次"" << strFuzzyFind << ""的其中一个字符" << endl;
cout << endl;
int times2 = FuzzyFindTimes(str2, strFuzzyFind, 2);
if (times2 == 0)
cout << "未能在"" << str2 << ""中找到" << strFuzzyFind
<< ""的任何一个字符" << endl;
else
cout << strFuzzyFind << " 在"" << str2 << ""里一共出现了"
<< times2 << "次"" << strFuzzyFind << ""的其中一个字符" << endl;
getch();
break;
}/*End Whilw*/
// 截取字符串
while (strChoose == "4")
{
system("cls");
cout << "您选择了进行截取字符串的操作" << endl
<< "该功能可以截取主字符串中从pos开始的n个字符的子串" << endl
<< " 第一行确认截取的是哪一个字符串(输入1或2);"
<< " 第二行输入两个数:" << endl
<< " 第一个数确认开始截取字符串的下标(即pos),"
<< "第二个数输入从pos开始截取的字符数(即n)" << endl
<< " 注:若第二项(n)为0,将自动截取从pos开始"
<< "到末尾的所有字符的子串" << endl << endl
<< " 字符串1:" << str1
<< (str1.empty() ? "(空)" : "") << endl
<< " 字符串2:" << str2
<< (str2.empty() ? "(空)" : "") << endl << endl;
cout << "请输入:" << endl;
int iChooseStr, pos, n;
cin >> iChooseStr >> pos >> n;
string strSub;
if (iChooseStr == 1)
{
if (n != 0)
strSub = str1.substr(pos - 1, n);
else strSub = str1.substr(pos - 1);
}
else if (iChooseStr == 2)
{
if (n != 0)
strSub = str2.substr(pos - 1, n);
else strSub = str1.substr(pos - 1);
}
else
{
cout << "您输入的数据不合法,请按下任意键后重新输入" << endl;
getch();
continue;
}
cout << "截取后的字符串为:" << endl;
cout << strSub << endl;
getch();
break;
}/* End while */
// 替换字符串(花的时间最多的功能之一)
while (strChoose == "5")
{
cin.clear();
system("cls");
cout << "您选择了进行替换字符串的操作" << endl
<< "该功能是将s1替换从pos开始的n个字符的子串" << endl << endl
<< "一共输入两行:" << endl
<< " 第一行输入用于替换已确认区间源字符串的字符串" << endl
<< " 第二行三个数:第一个数确认进行替换操作的是哪一个字符串"
<< "(输入 1 或 2);第二个数输入开始替换的下标(即pos),"
<< "第三个数输入的是被替换的字符串的长度(即n)" << endl
<< "注:若第一项(n)为0,将自动替换从第pos字符到末尾的字符串"
<< endl << endl
<< " 字符串1:" << str1
<< (str1.empty() ? "(空)" : "") << endl
<< " 字符串2:" << str2
<< (str2.empty() ? "(空)" : "") << endl << endl
<< "请输入:" << endl;
string stra = str1, strb = str2, s1;
int lena = stra.length(), lenb = strb.length(), iChooseStr, pos, n;
getline(cin, s1);
cin >> iChooseStr >> pos >> n;
if (iChooseStr == 1)
{
if (n != 0)
cout << endl << "替换后的字符串为:" << endl
<< stra.replace(pos - 1, n + 1, s1) << endl;
else
cout << endl << "替换后的字符串为:" << endl
<< stra.replace(pos - 1, lena - pos + 1, s1) << endl;
}
else if (iChooseStr == 2)
{
if (n != 0)
cout << endl << "替换后的字符串为:" << endl
<< strb.replace(pos - 1, n + 1, s1) << endl;
else
cout << endl << "替换后的字符串为:" << endl
<< strb.replace(pos - 1, lenb - pos + 1, s1) << endl;
}
else
{
cout << "您输入的数据不合法,请按下任意键后重新输入" << endl;
getch();
continue;
}
getch();
break;
}/* End while*/
// 查看字符串
while (strChoose == "6")
{
system("cls");
if (!str1.empty())
cout << "字符串1:" << str1 << " (长度:" << len1
<< " 个字符)" << endl;
if (!str2.empty())
cout << "字符串2:" << str2 << " (长度:" << len2
<< " 个字符)" << endl;
if (str1.empty())
cout << "字符串1:" << "(空)" << endl;
if (str2.empty())
cout << "字符串2:" << "(空)" << endl;
getch();
cout << endl << endl;
cout << " 注意事项" << endl
<< "1. 本程序的提示:xx字符在xx字符串的a位。<- 这里的a指的是"
<< "字符串从左往右数的第a个字符。举个例子:"abcdefg"这个字符串"
<< "的字符'a'存储在0位,这个没的说,然而程序会提示说是在第1位。"
<< "这是方便大家阅读,而采取的 实际位置+1 方法。所以'd'是在第4位"
<< "啦,可不是第3位哟!" << endl << endl
<< "2. 此程序版权所有作者" << endl
<< " 未经允许,严禁转载!" << endl
<< " 未经允许,严禁转载!" << endl
<< " 未经允许,严禁转载!" << endl;
cout << "确认后请按任意键……" ;
getch();
break;
}
// 更新内容
while (strChoose == "7")
{
system("cls");
cout << " 字符串操作 Beta 1.1.0 更新内容" << endl << endl
<< "║ 1. 功能2、3大改:现在可以统计出字符串出现的次数以及"
<< "每一次出现的位置了" << endl
<< "║ 2. 改动功能4、5:现在输入前显示的当前字符串会判断"
<< "字符串是否为空了" << endl
<< "║ 3. 新增更新内容(就是你现在看到的这个)" << endl
<< "║ 4. 改进功能6:修改判断字符串是否为空的方式" << endl;
getch();
break;
}
}/* End while */
return 0;
}
想和你说的一些话
在经过漫长的开发测试阶段,这个程序也终于迎来了公开测试阶段!这个程序耗时耗力巨大。从2020年8月初到今天,也有了将近一个月的“历史”。该程序行数已突破300行,长度突破10000,累计敲码30000+字,估计敲键盘得有五万次了。我每天至少要做上两个小时。可想而知,工作量得有多大!
再好的程序也难免会有Bug。据析,程序的开发测试阶段中,"一半是在完善功能,而另一半则是在修Bug!而且一个程序中,修复了一个Bug,又会产生另外更多的Bug。有几次,我甚至被Bug逼疯了,甚至想放弃了,放弃C++,放弃编程。但我一想,编程不就是要这种坚持不懈的精神吗?程序不就是用智慧的汗水凝结成的吗?于是我坚定信念,继续沿着编程这条路走下去。
在日益不停地肝代码中,我学会了很多,比如,统计字符串的次数、替换字符串等等。当然,我还有很多不足,毕竟我还是初学者,学的还只是那一点语言上的皮毛。更多高难度的知识等待着我去探索,特别是算法。希望大家和我一样去爱上编程,找到并探究编程的奥秘。
哦对了,最后要说的是:谢谢大家。
写在最后
作者:DawnNeon
若要转载请标明出处:https://www.cnblogs.com/DawnNeon/p/13563401.html
作者:黎明前的霓虹 若要转载请标明出处:https://www.cnblogs.com/DawnNeon/p/13563401.html

浙公网安备 33010602011771号