Advertisement
Stewie410

StringSplitter

Aug 14th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. # StringSplitter.py
  2. # Author:           Alex Paarfus <rapaarfus139@gmail.com>
  3. # Date:             2018-08-14
  4. #
  5. # Requirements:
  6. #   Prompt user for a string input
  7. #       Assume:
  8. #           Delimiter: <space>
  9. #           Multiple words/phrases
  10. #   Display the total number of characters (before loop)
  11. #   Display the total number of words (after loop)
  12. #   Per-word Stats:
  13. #       Word itself
  14. #       Length of Word
  15. #       NOTE: should probably not include punctuation
  16. #   Must use While loop
  17. #   Cannot use ".split()"
  18. #
  19. # Notes:
  20. #   I know, its a little OTT for the assignment
  21. #   I'm sure its inefficient
  22. #   Too bad, so sad.
  23.  
  24. # Get user input
  25. strInput = input("Enter several words seperated by spaces: ")
  26.  
  27. # Check for any input at all
  28. if not strInput:
  29.     print("Error: No input.")
  30. # Check to see if only a number was entered
  31. elif strInput.isnumeric():
  32.     print("Error: Numbers only.")
  33. else:
  34.     # Declare some vars to be used in our loop
  35.     i = 0
  36.     leftpos = 0
  37.     wordCount = 0
  38.     strTmp = ""
  39.     # Display the total number of characters in strInput
  40.     print("The length of the input string is: " + str(len(strInput)))
  41.     while (i < len(strInput)):
  42.         # if character is a letter
  43.         if (strInput[i].isalpha()):
  44.             # Add the letter to strTmp
  45.             strTmp += strInput[i]
  46.         # If character is a hyphen && i is larger than 0
  47.         elif ((strInput[i] == "-") and (i > 0)):
  48.             # And if (i-1) and (i+1) are both letters
  49.             if ((strInput[i - 1].isalpha()) and (strInput[i + 1].isalpha())):
  50.                 # Add the hypen to strTmp
  51.                 strTmp += strInput[i]
  52.         # If end of word
  53.         else:
  54.             # if strTmp contains at least one character
  55.             if len(strTmp) > 0:
  56.                 # print word stats
  57.                 # Print the actual word
  58.                 print("Word:          " + strTmp)
  59.                 # Print the Starting position
  60.                 print("Word LeftPos:  " + str(leftpos))
  61.                 # Print the Ending position
  62.                 print("Word RightPos: " + str(i))
  63.                 # Print the Length of the word
  64.                 print("Word Length:   " + str(len(strTmp)))
  65.                 # Print a blank line (seperator)
  66.                 print("")
  67.                 leftpos = i + 1
  68.                 wordCount += 1
  69.                 strTmp = ""      
  70.         i += 1
  71.         # End of the sentence handler
  72.         if (i >= len(strInput)):
  73.             if len(strTmp) > 0:
  74.                 # print word stats
  75.                 # Print the actual word
  76.                 print("Word:          " + strTmp)
  77.                 # Print the Starting position
  78.                 print("Word LeftPos:  " + str(leftpos))
  79.                 # Print the Ending position
  80.                 print("Word RightPos: " + str(i))
  81.                 # Print the Length of the word
  82.                 print("Word Length:   " + str(len(strTmp)))
  83.                 # Print a blank line (seperator)
  84.                 print("")
  85.                 leftpos = i + 1
  86.                 wordCount += 1
  87.                 strTmp = ""
  88.             break
  89.     print("Found " + str(wordCount) + " words.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement