编写函数实现python1.输入一字符串,各个子串之间按空白字符隔开,分别显式其中最长、最短子串,以及最大、最小字符。 点击复制后,将打开C知道体验页
时间: 2023-05-26 16:05:34 浏览: 131
def string_analysis(string):
substrings = string.split() # 将字符串按空白字符分隔成子串
max_len = 0
min_len = len(string)
max_char = ''
min_char = '~' # ASCII码表中~是最小字符可打印字符
for substring in substrings:
if len(substring) > max_len:
max_len = len(substring)
max_substring = substring
if len(substring) < min_len:
min_len = len(substring)
min_substring = substring
for char in substring:
if char > max_char:
max_char = char
if char < min_char:
min_char = char
print("最长子串:", max_substring)
print("最短子串:", min_substring)
print("最大字符:", max_char)
print("最小字符:", min_char)
# 测试
string_analysis("hello world hi")
# 输出:
# 最长子串: world
# 最短子串: hi
# 最大字符: w
# 最小字符:
阅读全文