Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####################################
- ## KR LAB 1 - STRING OPERATIONS ##
- ####################################
- ##########################
- ## 1. CHARACTER COUNT ##
- ##########################
- def charcount():
- word = input("Enter a word: ")
- count = 0
- for char in word:
- count += 1
- print(count)
- # charcount()
- ###############################
- ## 2. WORD WITH MAX LENGTH ##
- ###############################
- def maxLengthWord():
- statement = input("Enter a sentence: ")
- statement = statement.split(" ")
- max = 0
- for i in statement:
- if len(i) >= max:
- max = len(i)
- word = i
- print("The word with max length is: " + word)
- print("and its length is: " + str(max))
- # maxLengthWord()
- ######################################
- ## 3. FREQUENCY OF A GIVEN LETTER ##
- ######################################
- def getLetterFrequency():
- word = input("Enter a word: ")
- letter = input("Enter the letter to be checked: ")
- count = 0
- for i in word:
- if i == letter:
- count += 1
- print("Frequency of the letter " + letter + " in the given string is: " + str(count))
- # getLetterFrequency()
- ####################################
- ## 4. FREQUENCY OF EVERY LETTER ##
- ####################################
- def getEachLetterFrequency():
- string = input("Enter a string: ")
- dstring = ""
- for i in string:
- if i not in dstring:
- dstring += i
- for letter in dstring:
- count = 0
- for i in string:
- if i == letter:
- count += 1
- print("Frequency of the letter " + letter + " is: ", count)
- # getEachLetterFrequency()
- ##################################################
- ## 5. FREQUENCY OF A GIVEN WORD IN A SENTENCE ##
- ##################################################
- def wordFrequency():
- string = input("Enter a string: ")
- string = string.split(" ")
- tocheck = input("Enter the word: ")
- count = 0
- for word in string:
- if word == tocheck:
- count += 1
- print("Frequency of the word " + tocheck + " is " + str(count))
- # wordFrequency()
- ###########################
- ## 6. PALINDROME CHECK ##
- ###########################
- def isPalindrome():
- string = input("Enter a word: ")
- i = 0
- j = (len(string) - 1)
- while (i <= (len(string) / 2)):
- if string[i] == string[j]:
- flag = 1
- else:
- flag = 0
- break
- i += 1
- j -= 1
- if flag:
- print("YES! It is a palindrome")
- else:
- print("NO! Not a palindrome")
- #isPalindrome()
- ################################################
- ## 7. FREQUENCY OF EVERY WORD IN A SENTENCE ##
- ################################################
- def wordFrequency():
- string = input("Enter a string: ")
- dict = {}
- dstring = []
- for i in string.split(" "):
- if i not in dstring:
- dstring.append(i)
- for word in dstring:
- count = 0
- for i in string.split(" "):
- if i == word:
- count += 1
- dict[word] = count
- # print("Frequency of the word [" + word + "] is: ", count)
- print("The required frequencies are: ")
- print(dict)
- #wordFrequency()
- ################################
- ## 8. SUBSTRING IN A STRING ##
- ################################
- # ??? Split the mainstr into smaller strings of length equal to the substring's length
- # and chuck them into an array and then check if it exists.
- def isSubstring():
- mainstring = input("Enter a string: ")
- substring = input("Enter a substring: ")
- sl = len(substring)
- ml = len(mainstring)
- splits = {}
- flag = 0
- for i in range (0, (ml - sl + 1)):
- word = ""
- for j in range(i, (i + sl)):
- word += mainstring[j]
- splits[i] = word
- for key, value in splits.items():
- if value == substring:
- flag = 1
- position = key
- break
- if flag == 1:
- print("SUCCESS!")
- print("Given substring EXISTS in the given string starting from Index: " + str(position))
- else:
- print("ERROR!")
- ("Substring DOES NOT EXIST in the given string")
- #isSubstring()
Advertisement