编写函数any(s1,s2),将字符串s2中任一字符在字符串s1中第一次出现的位置作为结果返回。

本文介绍了一个C语言函数,用于查找字符串s2中任一字符在字符串s1中首次出现的位置。如果s1中不包含s2中的字符,则返回-1。通过遍历两个字符串,函数能够高效地定位字符位置。

编写函数any(s1,s2),将字符串s2中任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1.

拿着那本书随便做做

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

signed int buff[32]={0};

void any(char s1[],char s2[])
{
	int i,j,k=0;
	char flag=0;
	for(i=0;s2[i]!='\0';++i)
	{
		for(j=0;s1[j]!='\0' && !flag ;++j)
		{
			if(s2[i]==s1[j])
			{
				flag=1;
			}
		}
		if(flag)
		{
			buff[k++]=j;
		}else
		{
			buff[k++]=-1;
		}
		flag=0;		
	}
	buff[k]='\0';
}

void main()
{
	int i,length;
	char *s1[]=
	{
		" ",
		"a",
		"antidisestablishmentarianism",
		"beautifications",
		"characteristically",
		"deterministically",
		"electroencephalography",
		"familiarisation",
		"gastrointestinal",
		"heterogeneousness",
		"incomprehensibility",
		"justifications",
		"knowledgeable",
		"lexicographically",
		"microarchitectures",
		"nondeterministically",
		"organizationally",
		"phenomenologically",
		"quantifications",
		"representationally",
		"straightforwardness",
		"telecommunications",
		"uncontrollability",
		"vulnerabilities",
		"wholeheartedly",
		"xylophonically",
		"youthfulness",
		"zoologically"
	};
	char *s2[]=
	{
		" ",
		"a",
		"the",
		"quick",
		"brown",
		"dog",
		"jumps",
		"over",
		"lazy",
		"fox",
		"get",
		"rid",
		"of",
		"windows",
		"and",
		"install",
		"linux"
	};
	size_t s1num=sizeof s1/sizeof s1[0];
	size_t s2num=sizeof s2/sizeof s2[0];
	size_t s1_num,s2_num;
	for(s1_num=0;s1_num<s1num;s1_num++)
	{
		for(s2_num=0;s2_num<s2num;s2_num++)
		{
			any(s1[s1_num],s2[s2_num]);
			printf("%s  %s  ",s1[s1_num],s2[s2_num]);
			length=strlen(s2[s2_num]);
			for(i=0;i<length;i++)
				printf("%d ",buff[i]);
			putchar('\n');
		}
	}
}


fox 在 youyhfulness f第一次出现在 6 这个位子 o在2这个位子 x不存在所以返回-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值