字符串类的设计与实现C++

  • Post author:
  • Post category:其他


数据结构课程设计之字符串类的设计与实现


主要实现了字符串显示、查找指定字符、插入字符、删除字符、替换字符、字符串的拼接、求子串以及模式匹配BF算法八大功能。

编译器为Devc++。实现语言为C++。


主要思想:

我们在主函数输入了一个字符串,将其转换为字符数组,并获取该字符数组的长度。

这样我们就可以轻松地进行字符串类的设计与实现功能。

#include <iostream>
#include <bits/stdc++.h> 
using namespace std;
//字符串显示 
void ShowString(char array[],int length){
	for(int i=0;i<length;i++){
		cout<<array[i];
	}
	cout<<endl;
}
//查找指定字符
void FindString(char array[],int length) {
	cout<<"输入要查找的字符"<<endl;
	char c;
	int j = 0;
	int num=0;
	bool h = false;
	int location[100];
	cin>>c;
	for(int i=0;i<length;i++){
		if(array[i]==c){
			location[j]=i;
			h=true;
			num++;
			j++;
		}
	}
	if (h) {
		cout<<"该字符在字符串中出现的位置:"<<endl;
		for (int i = 0; i < j; i++) {
			cout<<location[i]+1<<endl;
		}
		cout<<"共有"<<num<<"个"<<endl; 
	}
	else {
	     cout<<"未找到该字符"<<endl;
	}
	cout<<endl;
}
//插入字符
void InsertString(char array[],int length) {
	int len = length;
	char c;
	int location;
	cout<<"插入的字符"<<endl; 
	cin>>c;
	cout<<"插入的位置"<<endl;
	cin>>location;
	if (location-1 > length) {
		cout<<"位置输入有误"<<endl;
		return;
	}
	for (; (len-1) != (location-2); len--) {
		array[len]=array[len-1];
	}
	array[location-1]=c;
	length++;
	cout<<"插入成功"<<endl;
	ShowString(array,length);
	cout<<endl;
}
//删除字符
void DeleteString(char array[],int length) {
	char c;
	int j;
	bool h = false;
	cout<<"删除的字符"<<endl;
	cin>>c;
	for (int i = 0; i < length; i++) {
		if (array[i] == c) {
			h = true;
			j = i;
			for (; j < length; j++) {
				array[j] = array[j + 1];
			}
			i--;
			length--;
		}
	}
	if (h) {
		cout<<"删除成功" <<endl;
		ShowString(array,length);
	}
	else {
		cout<<"未查找到需要删除的字符"<<endl;
	}
	cout<<endl;
}
//替换字符
void ReplaceString(char array[],int length) {
	char oldchar;
	char newchar;
	int j;
	bool h = false;
	cout<<"需要替换的字符"<<endl;
	cin>>oldchar;
	cout<<"替换之后的字符"<<endl;
	cin>>newchar;
	for (int i = 0; i < length; i++) {
		if (array[i] == oldchar) {
			h = true;
			array[i] = newchar;
		}
	}
	if (h) {
		cout<<"替换成功"<<endl;
		ShowString(array,length);
	}
	else {
		cout<<"该字符串中没有需要替换的字符"<<endl;
	}
	cout<<endl;
}
//串拼接
void TogetherString(char array[],int length) {
	char S1[100], S2[100], T[100];
	int i = 0, j = 0;
	cout<<"输入字符串s1"<<endl;
	cin>>S1;
	cout<<"输入字符串s2"<<endl; 
	cin>>S2;
	while (S1[i] != '\0') {
		T[i] = S1[i];
		i++;
	}
	while (S2[j] != '\0') {
		T[i] = S2[j];
		i++;
		j++;
	}
	if (i + j - 1 > 100) {
		cout<<"拼接失败,字符串过长"<<endl; 
		return;
	}
	T[i] = '\0';
	i = 0;
	cout<<"拼接成功"<<endl;
	while (T[i] != '\0') {
		cout<<T[i];
		i++;
	}
	cout<<endl;
}
//求子串
void FindSonString(char array[],int length) {
	char Sub[100];
	int len, pos,i=0;
	cout<<"输入子串的起始位置"<<endl;
	cin>>pos;
	cout<<"输入子串的长度"<<endl;
	cin>>len;
	int lastLocation=pos - 1 + len;
	for (; pos <= lastLocation; pos++) {
		Sub[i]=array[pos-1];
		i++;
		Sub[i] = '\0';
	}
	cout<<"子串:"<<endl;
	i = 0;
 
	while (Sub[i] != '\0') {
		cout<<Sub[i]; 
		i++;
	}
	cout<<endl;
}
//模式匹配BF算法
void MatchedBFAlgorithm(char array[],int length) {
	char T[100];
	int start;
	cout<<"输入子串"<<endl;
	cin>>T;
	int sonLen = 0,fatherLen=0;
	while ((sonLen != strlen(T)) && (fatherLen != length)) {
		if (array[fatherLen] == T[sonLen]) {
			fatherLen++;
			sonLen++;
		}
		else {
			sonLen = 0;
			fatherLen++;
		}
	}
	if (sonLen == strlen(T)) {
		start=fatherLen - sonLen;
		cout<<"子串的起始位置:"<<start+1<<endl;
	}
	else {
		cout<<"没有该子串"<<endl;
	}
	cout<<endl; 
}



int main(){
	string s;
	cout<<"输入字符串"<<endl;
	cin>>s;
	//字符串转换为字符数组array
    char array[100];
    int length = s.copy(array, 99);
    array[length] ='\0';
    int num;
    while (1) {
    	//system("cls");
		cout<<"1、显示字符串信息"<<endl;
		cout<<"2、查找指定字符\n"<<endl;
		cout<<"3、插入字符"<<endl;
		cout<<"4、删除字符"<<endl;
		cout<<"5、替换字符"<<endl;
		cout<<"6、串拼接"<<endl;
		cout<<"7、求子串"<<endl;
		cout<<"8、模式匹配BF算法"<<endl;
		cout<<"请输入需要的功能序号:"<<endl;
		cin>>num;
		switch (num) {
		case 1:
			ShowString(array,length);
			system("pause");
			break;
		case 2:
			FindString(array,length);
			system("pause");
			break;
		case 3:
			InsertString(array,length);
			system("pause");
			break;
		case 4:
			DeleteString(array,length);
			system("pause");
			break;
		case 5:
			ReplaceString(array,length);
			system("pause");
			break;
		case 6:
			TogetherString(array,length);
			system("pause");
			break;
		case 7:
			FindSonString(array,length);
			system("pause");
			break;
		case 8:
			MatchedBFAlgorithm(array,length);
			system("pause");
			break;
		default:
			printf("输入错误,请重新输入!\n");
			system("pause");
			break;
		}
	}
	return 0;
}

核心代码参考于

(86条消息) 字符串处理系统的设计与实现(C语言)_基础不扎实的计算机小白的博客-CSDN博客_字符串处理程序设计



版权声明:本文为weixin_51626421原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。