Computer Science, asked by moumitadas17644, 4 months ago

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 SherlockHolmes221b
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:

  1. The input string is taken
  2. The length of the string is calculated
  3. msg variable is declared with value "Unique"
  4. A for-loop iterates through each index of string
  5. Nested for-loop iterates through each index after the ith letter
  6. If statement checks if the ith letter equals to the jth letter of unknown_str
  7. If the ith letter and jth letter of unknown_str are equal (i.e., the letter is repeated implies that string is not unique)
  8. Set the msg variable's value to "Not Unique"
  9. Print the variable msg
Similar questions