Advertisement
Stewie410

StringSplitterLimted

Aug 15th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. # StringSplitterLimted.py
  2. # Author:           Alex Paarfus <rapaarfus139@gmail.com>
  3. # Date:             2018-08-15
  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. # Preface statement
  25. print("===============================================================================================")
  26. print("== This program will accept a sentence (or similar list of words) as an input                ==")
  27. print("== Before iterating through the input, it will print out the total number of characters.     ==")
  28. print("== Next, it will iterate through the input and locate all of the words in the input.         ==")
  29. print("== When a word is found, the program will list the starting and ending positions of the word ==")
  30. print("== as well as the word itself and the length of the word.                                    ==")
  31. print("== Finally, the program will print the total number of words found.                          ==")
  32. print("===============================================================================================")
  33.  
  34. # Get user input
  35. strInput = input("Enter a sentence: ")
  36.  
  37. # Check for user input
  38. if strInput:
  39.     # Declare some variables to be used in our loop
  40.     i = 0
  41.     leftpos = 0
  42.     wordCount = 0
  43.     strTmp = ""
  44.     alphas = "abcdefghijklmnopqrstuvwxyz"
  45.  
  46.     # Display the total number of characters in strInput
  47.     print("The length of the input string is: " + (str(len(strInput))))
  48.    
  49.     # Iterate through the input, extracting each word
  50.     while (i < len(strInput)):
  51.         # Check to see if charAt(i) is an alphabetic character
  52.         j = 0
  53.         blnAlpha = False
  54.         while (j < len(alphas)):
  55.             if (strInput[i] == alphas[j]):
  56.                 # set out Alpha flag to true
  57.                 blnAlpha = True
  58.                 # break our of the checkAlpha loop
  59.                 break
  60.             j += 1
  61.         # If char is an alpha, add it to our temporary string
  62.         if (blnAlpha):
  63.             strTmp += strInput[i]
  64.         else:
  65.             # If strTmp contains characters, or if at the end of the string
  66.             if ((len(strTmp) > 0) or ((i + 1) >= len(strInput))):
  67.                 # Print the word
  68.                 print("Word     = " + strTmp)
  69.                 # Print the left and right positions of the word
  70.                 print("Leftpos  = " + str(leftpos))
  71.                 print("Rightpos = " + str(i))
  72.                 # Print the length of the word
  73.                 print("Length   = " + str(len(strTmp)))
  74.                 # Update the leftpos value to whatever "i" is
  75.                 leftpos = i
  76.                 # Increment the word count by 1
  77.                 wordCount += 1
  78.                 # Reset the value of strTmp to an empty string
  79.                 strTmp = ""
  80.         # Increment i by 1
  81.         i += 1
  82.     # Print the numbers of words found
  83.     print("Found " + str(wordCount) + " words.")
  84. else:
  85.     # Display the error message for no input
  86.     print("Error: No input.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement