두 개의 문자열을 가져와서 내장 함수를 사용하지 않고 더 큰 문자열을 표시해야 하는 경우 간단한 반복과 '==' 연산자를 사용할 수 있습니다.
아래는 동일한 데모입니다 -
예
string_1 = "Malala"
string_2 = "Male"
count_1 = 0
count_2 = 0
print("The first string is :")
print(string_1)
print("The second string is :")
print(string_2)
for i in string_1:
count_1 = count_1+1
for j in string_2:
count_2 = count_2+1
if(count_1<count_2):
print("The larger string is:")
print(string_2)
elif(count_1==count_2):
print("Both the strings are equal.")
else:
print("The larger string is:")
print(string_1) 출력
The first string is : Malala The second string is : Male The larger string is: Malala
설명
-
두 개의 문자열이 정의되어 콘솔에 표시됩니다.
-
두 개의 카운터가 0으로 초기화됩니다.
-
문자열이 반복되고 길이가 얻어집니다.
-
이것은 증가되어 카운터에 저장됩니다.
-
카운트 값에 따라 두 문자열 중 더 큰 문자열이 콘솔에 표시됩니다.