A string is said to be 'Unique' if none of the letters present in the string are repeated.
Write a program to accept a string and check whether the string is Unique or not. The
program displays a message accordingly.
Sample Input: COMPUTER
Sample Output: Unique String
Answers
Answered by
2
Answer:
unknown_str = input("Enter a string: ")
length_of_str = len(unknown_str)
msg = "Unique"
for i in range(length_of_str):
for j in range(i+1,length_of_str):
if unknown_str[i] == unknown_str[j]:
msg = "Not Unique"
print(msg)
Explanation:
- The input string is taken
- The length of the string is calculated
- msg variable is declared with value "Unique"
- A for-loop iterates through each index of string
- Nested for-loop iterates through each index after the ith letter
- If statement checks if the ith letter equals to the jth letter of unknown_str
- If the ith letter and jth letter of unknown_str are equal (i.e., the letter is repeated implies that string is not unique)
- Set the msg variable's value to "Not Unique"
- Print the variable msg
Similar questions