栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

主要思想:

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

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

#include 
#include  
using namespace std;
//字符串显示 
void ShowString(char array[],int length){
	for(int i=0;ic;
	for(int i=0;i>c;
	cout<<"插入的位置"<>location;
	if (location-1 > length) {
		cout<<"位置输入有误"<>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<<"删除成功" <>oldchar;
	cout<<"替换之后的字符"<>newchar;
	for (int i = 0; i < length; i++) {
		if (array[i] == oldchar) {
			h = true;
			array[i] = newchar;
		}
	}
	if (h) {
		cout<<"替换成功"<>S1;
	cout<<"输入字符串s2"<>S2;
	while (S1[i] != '') {
		T[i] = S1[i];
		i++;
	}
	while (S2[j] != '') {
		T[i] = S2[j];
		i++;
		j++;
	}
	if (i + j - 1 > 100) {
		cout<<"拼接失败,字符串过长"<>pos;
	cout<<"输入子串的长度"<>len;
	int lastLocation=pos - 1 + len;
	for (; pos <= lastLocation; pos++) {
		Sub[i]=array[pos-1];
		i++;
		Sub[i] = '';
	}
	cout<<"子串:"<>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<<"子串的起始位置:"<>s;
	//字符串转换为字符数组array
    char array[100];
    int length = s.copy(array, 99);
    array[length] ='';
    int num;
    while (1) {
    	//system("cls");
		cout<<"1、显示字符串信息"<>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博客_字符串处理程序设计 

如若侵权,请作者私信,侵权必删。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980101.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号