阅读下列说明和C函数,填补C函数中的空缺,将解答填入答案纸的对应栏目内。
字符串是程序中常见的一种处理对象,在字符串中进行子串的定位、插入和删除是常见的运算。
设存储字符串时不设置结束标志,而是另行说明串的长度,因此串类型定义如下:

【函数1说明】
函数indexStr(S,T,pos)的功能是:在S所表示的字符串中,从下标pos开始查找T所表示字符串首次出现的位置。方法是:第一趟从S中下标为pos、T中下标为0的字符开始,从左往右逐个来比较S和T的字符,直到遇到不同的字符或者到达T的末尾。若到达T的末尾,则本趟匹配的起始下标pos为T出现的位置,结束查找;若遇到了不同的字符,则本趟匹配失效。下一趟从S中下标pos+1处的字符开始,重复以上过程。若在S中找到T,则返回其首次出现的位置,否则返回-1。
例如,若S中的字符串为“students ents”,T中的字符串为“ent”,pos=0,则T在S中首次出现的位置为4。

【函数2说明】
函数eraseStr(S,T)的功能是删除字符串S中所有与T相同的子串,其处理过程为:首先从字符串S的第一个字符(下标为0)开始查找子串T,若找得到子串T在S中的起始位置,则将串S中子串T之后的所有字符向前移动,将子串T覆盖,从而将其删除,然后重新开始查找下一个子串T,若找到就用后面的字符序列进行覆盖,重复上述过程,直到将S中所有的子串T删除。
例如,若字符串S为“12ab345abab678”、T为“ab”。第一次找到“ab”时(位置为2),将“345abab678”前移,S中的串改为“12345abab678”;第二次找到“ab”时(位置为5);将ab678前移,S中的串改为“12345ab678”;第三次找到“ab”时(位置为5),将“678”前移,S中的串改为“12345678”。

正确答案及解析
正确答案
解析
(1)i-j+1
(2)j==T.length
(3)S,T,pos
(4)i-T.length
(5)S->length-T.length
解析:函数1为字符串匹配,算法为:先判断字符串S和T的长度,如果为空则不用循环,另外,如果字符串S的长度小于字符串T的长度,那字符串S中也不能含有字符串T,也无需进行匹配。
(1)当上述情况都不存在时,即需要进行循环。从S的第一个字符开始,与T的第一个字符进行比较,如果相等,则S的第二个字符和T的第二字符进行比较,再相等就再往后移动一位进行比较,依次直到字符串T的结尾;当某一个字符与T的字符不相等时,那么字符串S就应从下一个字符开始比较,此时i=i-j+1(如果前面有匹配成功的话,i的值已经增加了j位,因此需要重新回到之前比较的位置的后一个字符进行比较),故(1)中应填i-j+1;
(2)当匹配成功时,j的元素应等于T串的长度,故应填j==T.length。
函数2为字符串的删除运算。
(3)首先,要调用函数indexStr,显然需要三个参数,字符串S、字符串T和pos,故(3)中应填S,T,pos;
(4)找到子串T的位置后,使用覆盖的方法删除T,即将覆盖的所有元素向前移T的长度,则(4)中应填i-T.length;
(5)而删除T字符串后,字符串S的总长度变化,需减去字符串T的长度,故(5)中应填S->length-T.length。
你可能感兴趣的试题
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答5处
-
- A.sensor
- B.searching
- C.service
- D.source
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答4处
-
- A.low-level
- B.low-cost
- C.high-cost
- D.high-performance
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答3处
-
- A.19th
- B.20th
- C.21th
- D.22th
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答2处
-
- A.path
- B.Internet
- C.route
- D.switch
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答1处
-
- A.this
- B.thing
- C.think
- D.things
- 查看答案