C:数据结构sy7

devtools/2024/9/23 1:33:16/

【问题描述】
编写一函数strend(s, t),如果字符串t出现在字符串s的尾部,该函数返回1,否则返回0。要求在main函数中测试该函数:先从键盘输入字符串s、t(不超过100个字符),然后调用该函数,并输出返回的结果。
【输入形式】
从键盘分行输入两个字符串:s,t
【输出形式】
输出有两种可能,Yes或No
【输入样例】
abcdefgh
fgh
【输出样例】
Yes

【样例说明】
输入了两个字符串s,t,发现t是在s的尾部,则输出Yes

#include <stdio.h>
#include <string.h>#define MaxSize 100
typedef char ElementType;typedef struct
{int length;ElementType data[MaxSize];
}SString;
int SString_tail(SString s,SString t)
{int tail=s.length-t.length;if(tail<0||t.length==0){return 0;}int i;for ( i = 0; i <t.length ; i++) {if(s.data[tail++]!=t.data[i]){return 0;}}return 1;
}
void PrintSString(SString L)
{int i;for ( i = 0; i < L.length; i++) {printf("%3c",L.data[i]);}printf("\n");
}int main() {SString s,t;fgets(s.data,100,stdin);fgets(t.data,100,stdin);s.length=strlen(s.data);t.length=strlen(t.data);
//    PrintSString(s);
//    PrintSString(t);int ret;ret=SString_tail(s,t);if(ret){printf("Yes");} else{printf("No");}return 0;
}

【问题描述】用简单模式匹配算法判断模式串T是否在主串S的一个给定位置(pos)开始的串中,若在,则输出模式串在主串的第pos位置开始的匹配位置,否则,匹配失败,返回零值。

【输入形式】主串S,模式串T,起始位置pos
【输出形式】匹配的相关信息
【样例输入1】

    Please input the Main SString S:aabaabaabaac

    Please input the other SString T:aabaac

    the start position:5

【样例输出1】

    the index result is:

    The postion of T in the Main SString S from 5  chars on is: 7

【样例输入2】

    Please input the Main SString S:aabaabaabaac

    Please input the other SString T:aabaac

    the start position:8

#include <stdio.h>
#include <string.h>
int  Index(char  S[],char  T[],int  pos)
{int i=0,j=pos-1;int ls=strlen(S)-1,lt=strlen(T)-1;if(lt>ls||lt==0||ls==0){return 0;}while (i<=lt&&j<=ls){if(S[j]==T[i]){i++,j++;if(i==lt){return j-lt+1;}} else{j=j-i+1;i=0;}}return 0;
}
int  main() {char S[100], T[100];int pos;printf("Please input the Main SString S:");fgets(S,100,stdin);printf("Please input the other SString T:");fgets(T,100,stdin);printf("the start position:");scanf("%d", &pos);int ret = Index(S, T, pos);if (ret) {printf("\nthe index result is:\nThe postion of T in the Main SString S from %d  chars on is: %d", pos, ret);} else {printf(" \nthe index result is:\nFailure to Index the SString!");}return 0;}


http://www.ppmy.cn/devtools/3909.html

相关文章

Alibaba --- 如何写好 Prompt ?

如何写好 Prompt 提示工程&#xff08;Prompt Engineering&#xff09;是一项通过优化提示词&#xff08;Prompt&#xff09;和生成策略&#xff0c;从而获得更好的模型返回结果的工程技术。总体而言&#xff0c;其实现逻辑如下&#xff1a; &#xff08;注&#xff1a;示例图…

C#生成一个绿色文件

生成一个绿色文件免去了安装的繁琐过程&#xff0c;直接运行&#xff0c;非常方便。 新建一个类库项目 在类库Class1中实现简单的Sum方法。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespac…

如何用JAVA如何实现Word、Excel、PPT在线前端预览编辑的功能?

背景 随着信息化的发展&#xff0c;在线办公也日益成为了企业办公和个人学习不可或缺的一部分&#xff0c;作为微软Office的三大组成部分&#xff1a;Word、Excel和PPT也广泛应用于各种在线办公场景&#xff0c;但是由于浏览器限制及微软Office的不开源等特性&#xff0c;导致…

【Redis常见问题总结】

文章目录 1、redis介绍2、redis常见的集合类型4、思考redis为什么这么快redis实现单线程模型、redis 实现IO多路复用 5、redis持久化AOF的实现过程RDB的实现过程 6、redis集群redis集群的搭建redis集群的原理redis集群如何分区Redis集群的原理 7、 redis过期删除和淘汰策略8、r…

达梦数据库的DMRMAN工具介绍

达梦数据库的DMRMAN工具介绍 DMRMAN&#xff08;DM RECOVERY MANAGER&#xff09;是 DM 的脱机备份还原管理工具&#xff0c;由它来统一负责库级脱机备份、脱机还原、脱机恢复等相关操作&#xff0c;该工具支持命令行指定参数方式和控制台交互方式执行&#xff0c;降低了用户的…

2024蓝桥杯每日一题(欧拉函数)

备战2024年蓝桥杯 -- 每日一题 Python大学A组 试题一&#xff1a;最大公约数 试题二&#xff1a;欧拉函数 试题三&#xff1a;筛法求欧拉函数 试题四&#xff1a;互质数的个数 试题五&#xff1a;可见的点 试题一&#xff1a;最大公约数 【题…

Web入门-Tomecat

黑马程序员JavaWeb开发教程 文章目录 一、简介1、Web服务器2、Tomcat 二、基本使用三、入门程序解析 一、简介 1、Web服务器 对HTTP协议操作进行封装&#xff0c;简化web程序开发部署Web项目&#xff0c;对外提供网上信息浏览服务 2、Tomcat 概念&#xff1a;Tomcat是Apach…

css元素显示模式

前言 本文为我在学习后总结的css中元素显示模式的相关知识点。&#xff08;学自b站黑马pink老师&#xff09; 概念介绍 规定这个标签以什么样的形式显示在页面上。通俗本质上&#xff0c;就是可以理解为&#xff0c;这个标签在页面上占一行&#xff0c;还是一小块位置。 主要…