头歌-数据结构-串的基本运算1-4

第1关:统计字符出现的频度

//第1关:统计字符出现的频度
#include <cstdio>
#include <iostream>
using namespace std;
void Count(char s[], int b[])
{  //统计串中的数字字符和大写字母的频度,并将结果存入数组b 
    /**************begin************/
    while (*s)
    {
        if (*s >= 48 && *s <= 57)
            b[*s - 48]++;
        else
            b[*s - 55]++;
        s++;
    }

    /**************end**************/
}
int main()
{
    char s[255];        // 定义串s
    cin >> s;             //输入串s
    int b[36] = { 0 };      //定义数组b,初始化为0 
    Count(s, b);

    //输出统计结果(只输出存在的字符,格式为 字符:频度),字符排列顺序按照ASCII码从小到大的顺序。 
    /**************begin************/
    int i;
    for (i = 0; i < 36; i++)
    {
        if (!b[i])
            continue;
        else
        {
            if (i < 10)
                printf("%c:%d\n", i + 48, b[i]);
            else
                printf("%c:%d\n", i + 55, b[i]);
        }
    }

    /**************end**************/
    return 0;
}

第2关:取子串

//第2关:取子串
#include <cstdio>
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;

#define MAXLEN 255
typedef struct{
	char ch[MAXLEN+1];		//从1号下标开始存储串值 
	int length;				//串的长度 
}SString;        			//串的定长顺序存储结构     

void InputStr(SString &s)	//输入串 
{	
    char str[MAXLEN];
	int i;
	cin>>str;
	for (i=0;str[i]!='\0';i++)
			s.ch[i+1]=str[i];
	s.length=i;
}

void OutputStr(SString s)	//输出串
{ 
	for (int i=1;i<=s.length;i++) 
		cout<<s.ch[i];	
}

Status SubString(SString s,SString &sub,int  pos,int len){
	//在串s的第pos位置开始取出len个字符,存入串sub,若成功,返回OK,否则返回ERROR 
	/********************begin********************/
 	int i=1,j=pos;
 	while(s.ch[pos]!='\0' && i<=len)
 	{
 		sub.ch[i++]=s.ch[pos++];
	}
	if(i-1!=len)
		return ERROR;
	
	sub.length=i-1;
	return OK;
		

  	/********************end ********************/
}


int main() 
{	
    SString s,sub;
    int pos,len; 
    InputStr(s);				//输入串s
	cin>>pos; 
	cin>>len;
	//调用取子串函数,并按要求输出。 
	/********************begin********************/
	if(!SubString(s,sub,pos,len))
		printf("Error!");
	else
	{
		for(pos=1;pos<=sub.length;pos++)
			printf("%c",sub.ch[pos]);
	}
	
	/********************end ********************/	
	return 0; 
}

第3关:串的模式匹配

//第3关:串的模式匹配
#include <iostream>
using namespace std;

#define MAXLEN 255
typedef struct {
	char ch[MAXLEN + 1];		//从1号下标开始存储串值 
	int length;				//串的长度 
}SString;        			//串的定长顺序存储结构     

void InputStr(SString& s)	//输入串 
{
	char str[MAXLEN];
	int i;
	cin >> str;
	for (i = 0; str[i] != '\0'; i++)
		s.ch[i + 1] = str[i];
	s.length = i;
}
int Index_BF(SString s, SString t)
{   //在串s中查找串t的首次出现位置,如不存在,则返回0 
	/***************begin*************/
	int i = 1;
	int j = 1;
	while (i <= s.length)
	{
		if (s.ch[i] != t.ch[j])
		{
			i++;
		}
		else
		{
			int k = ++i;
			j++;
			while (j <= t.length)
			{
				if (s.ch[k] == t.ch[j])
				{
					k++;
					j++;
				}
				else
				{
					j = 1;
					break;
				}
			}
			if (j > t.length)
			{
				return i - 1;
			}
			


		}
	}
	return 0;
	/***************end *************/
}

int main()
{
	SString s, t;
	InputStr(s);				//输入串s
	InputStr(t);				//输入串t
	cout << Index_BF(s, t);		//输出查找结果	
	return 0;
}

第4关:字符串的插入

//第4关:字符串的插入
#include <iostream>
using namespace std;
#define MAXLEN 255
typedef struct {
	char ch[MAXLEN + 1];		//从1号下标开始存储串值 
	int length;				//串的长度 
}SString;        			//串的定长顺序存储结构     

void InputStr(SString& s)	//输入串 
{
	char str[MAXLEN];
	int i;
	cin >> str;
	for (i = 0; str[i] != '\0'; i++)
		s.ch[i + 1] = str[i];
	s.length = i;
}
void OutputStr(SString s)	//输出串
{
	for (int i = 1; i <= s.length; i++)
		cout << s.ch[i];
}

void InsertStr(SString& s, int pos, SString t)
{  //在串s的第pos位置插入串t,如pos不合法,则不插入
	/***************begin*************/
	int i = 0;
	int j = 0;
	if (pos > s.length + 1 || pos<1)    //不合法位置插入
		return;

	else
	{
		i = s.length;
		j = 1;
		for (;j<=s.length-pos+1;i--,j++)   
		{
			s.ch[i + t.length] = s.ch[i];
		}
		for (i = pos, j = 1; j <= t.length;i++,j++)
		{
			s.ch[i] = t.ch[j];
		}
	}
	s.length = s.length + t.length;
	/***************end *************/
}

int main()
{
	SString s, t;
	int pos;
	InputStr(s);				//输入串s
	cin >> pos;					//输入插入位置 
	InputStr(t);				//输入串t
	InsertStr(s, pos, t);         //调用插入函数 
	OutputStr(s);				//输出串s 
	return 0;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值