给出两个字符串,如何计算第一个字符串中第二个字符串的出现次数 [英] Give two strings, how to compute the number of occurrences of second string in the first

查看:186
本文介绍了给出两个字符串,如何计算第一个字符串中第二个字符串的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该程序的代码.我需要帮助.

I don''t know the coding for this program. I need help please.

推荐答案

这听起来像是您的作业,所以我不会给您任何代码! :laugh:

它并不复杂,您所要做的就是把它当作是您在看的,而不是一台计算机.

您将使用第二个字符串(将其称为match),并在第一个字符串中查找第一个匹配项(将其称为source).然后,您会忽略源代码中直到找到的match开头的所有字符(还有一点点确保您没有再次找到相同的字符),然后寻找另一个字符.然后重复此操作,直到剩下的source的数量少于match

所以:这是我的做法:

创建一个名为IsMatch的函数-如果匹配则返回true,否则返回false.
它有两个参数,两个均为char *,第一个为source,第二个为match-我们将假定这两个参数都为null终止.
然后遍历字符串:
如果* source为null,则返回false.
如果* source与* match不同,则返回false.
否则,将sourcematch移一.
如果* match为null,则返回true.
否则,请重新循环.

然后,您要做的就是在另一个循环中调用它:
每次您转身时,均使用sourcematch进行调用.如果返回true,请增加计数.
将源向前移动一个.如果* source为null,则退出循环.


一切都完成了!
This sounds like your homework, so I''ll give you no code! :laugh:

It''s not complex, all you have to do is look at it as if it was you doing it, rather than a computer.

You would take the second string (lets call it match) and look for the first occurance in the first string (lets call it source). You would then ignore all the characters in the source up to the beginning of the match you found (plus a bit to make sure you didn''t find the same one again) and look for another. And then you would repeat this until teh amount of the source you had left was shorter than the match

So: here is how I would do it:

Create a function, called IsMatch - it returns true if it''s a match and false if it isn''t.
It takes two parameters, both char*, the first is the source, the second is the match - we will assume that both are null terminated.
Then loop through the strings:
if *source is null, return false.
if *source is not the same as *match, return false.
otherwise, move source and match one one.
if *match is null, return true.
otherwise, go back round the loop.

Then all you have to do is call it in another loop:
each time you go round, call it with source and match. If it returns true, increment your count.
Move source on by one. If *source is null, exit the loop.


All done!


看看联机帮助页:strtok和strtok_r [ ^ ],以分隔符和联机帮助页:索引 [联机帮助页:strstr [
Have a look at Manpage: strtok and strtok_r[^] that splits strings by delimiters and Manpage: index[^] that gets the occurance of a character in a string, and finally Manpage: strstr[^] to get a substring in a string.
Iterate over the larger string and count the number of loops.
Cheers
Andi


这篇关于给出两个字符串,如何计算第一个字符串中第二个字符串的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆