输入一字符串,各个子串之间按空白字符隔开,分别显式其中最长、最短子串,以及最大、最小字符用paython编写程序
时间: 2024-05-25 18:18:13 浏览: 108
string = input("请输入字符串:")
# 按空格分割字符串
substrings = string.split()
# 初始化最长和最短子串为第一个子串
longest_substring = substrings[0]
shortest_substring = substrings[0]
# 初始化最大和最小字符为第一个子串的第一个字符
max_char = substrings[0][0]
min_char = substrings[0][0]
# 遍历所有子串
for substring in substrings:
# 更新最长和最短子串
if len(substring) > len(longest_substring):
longest_substring = substring
if len(substring) < len(shortest_substring):
shortest_substring = substring
# 遍历子串中的所有字符
for char in substring:
# 更新最大和最小字符
if ord(char) > ord(max_char):
max_char = char
if ord(char) < ord(min_char):
min_char = char
# 输出结果
print("最长子串:", longest_substring)
print("最短子串:", shortest_substring)
print("最大字符:", max_char)
print("最小字符:", min_char)
阅读全文