jeapedu 26 字符串与两重循环习题2答案

本文介绍了一种使用双循环的方法来查找特定子串的位置,并演示了如何从给定的字符串中提取网址。此外,还展示了如何截取指定范围内的字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: https://pan.baidu.com/s/1smuD9Pb 密码: t8ir

# 1 find sub string "jeap" and pos
# 2 judge some string have any sub string
# tips: double cycle
# hello jeapedu.com jealop
# jeap
# 3 extract website
# s = "welcome to visit my website http://www.baidu.com hehe! or http
# ://www.163.com or http://www.sohu.com"
# extract website sub string baidu 163 sohu
# head = "www"  -> ph
# tail = ".com" -> pt

s = "welcome to visit my website x.com http://3.org http://www.baidu.com a.com cx.com hehe! or http://www.163.com or http://www.sohu.com"
head = "http://"
tail = ".com"
ph = 0
preh = 0
pt = 0
i = 0
while i < len(s):
    j = 0
    ch = 0
    while j < len(head) and i + j < len(s):
        if s[i + j] == head[j]:
            ch += 1
        j += 1
    if ch == len(head):
        ph = i
        print("ph =", ph, end=' ')

    j = 0
    ct = 0
    while j < len(tail) and i + j < len(s):
        if s[i + j] == tail[j]:
            ct += 1
        j += 1
    if ct == len(tail):
        pt = i
        print("pt =", pt, end=' ')
# when found tail, print it
#       print(s[ph + len(head) : pt + len(tail)])
        if ph < pt and preh != ph:
            k = ph
            dns = ""
            while k < pt + len(tail):
                dns += s[k]
                k += 1
            preh = ph # 暂存上次的头
            print(dns)
    i += 1

s = "hello jeapedu.com"
t = ""
k = 6
while k < 13:
    t += s[k]
    k += 1
print(s)
print(t)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值