Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # StringSplitter.py
- # Author: Alex Paarfus <rapaarfus139@gmail.com>
- # Date: 2018-08-14
- #
- # Requirements:
- # Prompt user for a string input
- # Assume:
- # Delimiter: <space>
- # Multiple words/phrases
- # Display the total number of characters (before loop)
- # Display the total number of words (after loop)
- # Per-word Stats:
- # Word itself
- # Length of Word
- # NOTE: should probably not include punctuation
- # Must use While loop
- # Cannot use ".split()"
- #
- # Notes:
- # I know, its a little OTT for the assignment
- # I'm sure its inefficient
- # Too bad, so sad.
- # Get user input
- strInput = input("Enter several words seperated by spaces: ")
- # Check for any input at all
- if not strInput:
- print("Error: No input.")
- # Check to see if only a number was entered
- elif strInput.isnumeric():
- print("Error: Numbers only.")
- else:
- # Declare some vars to be used in our loop
- i = 0
- leftpos = 0
- wordCount = 0
- strTmp = ""
- # Display the total number of characters in strInput
- print("The length of the input string is: " + str(len(strInput)))
- while (i < len(strInput)):
- # if character is a letter
- if (strInput[i].isalpha()):
- # Add the letter to strTmp
- strTmp += strInput[i]
- # If character is a hyphen && i is larger than 0
- elif ((strInput[i] == "-") and (i > 0)):
- # And if (i-1) and (i+1) are both letters
- if ((strInput[i - 1].isalpha()) and (strInput[i + 1].isalpha())):
- # Add the hypen to strTmp
- strTmp += strInput[i]
- # If end of word
- else:
- # if strTmp contains at least one character
- if len(strTmp) > 0:
- # print word stats
- # Print the actual word
- print("Word: " + strTmp)
- # Print the Starting position
- print("Word LeftPos: " + str(leftpos))
- # Print the Ending position
- print("Word RightPos: " + str(i))
- # Print the Length of the word
- print("Word Length: " + str(len(strTmp)))
- # Print a blank line (seperator)
- print("")
- leftpos = i + 1
- wordCount += 1
- strTmp = ""
- i += 1
- # End of the sentence handler
- if (i >= len(strInput)):
- if len(strTmp) > 0:
- # print word stats
- # Print the actual word
- print("Word: " + strTmp)
- # Print the Starting position
- print("Word LeftPos: " + str(leftpos))
- # Print the Ending position
- print("Word RightPos: " + str(i))
- # Print the Length of the word
- print("Word Length: " + str(len(strTmp)))
- # Print a blank line (seperator)
- print("")
- leftpos = i + 1
- wordCount += 1
- strTmp = ""
- break
- print("Found " + str(wordCount) + " words.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement