Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # StringSplitterLimted.py
- # Author: Alex Paarfus <rapaarfus139@gmail.com>
- # Date: 2018-08-15
- #
- # 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.
- # Preface statement
- print("===============================================================================================")
- print("== This program will accept a sentence (or similar list of words) as an input ==")
- print("== Before iterating through the input, it will print out the total number of characters. ==")
- print("== Next, it will iterate through the input and locate all of the words in the input. ==")
- print("== When a word is found, the program will list the starting and ending positions of the word ==")
- print("== as well as the word itself and the length of the word. ==")
- print("== Finally, the program will print the total number of words found. ==")
- print("===============================================================================================")
- # Get user input
- strInput = input("Enter a sentence: ")
- # Check for user input
- if strInput:
- # Declare some variables to be used in our loop
- i = 0
- leftpos = 0
- wordCount = 0
- strTmp = ""
- alphas = "abcdefghijklmnopqrstuvwxyz"
- # Display the total number of characters in strInput
- print("The length of the input string is: " + (str(len(strInput))))
- # Iterate through the input, extracting each word
- while (i < len(strInput)):
- # Check to see if charAt(i) is an alphabetic character
- j = 0
- blnAlpha = False
- while (j < len(alphas)):
- if (strInput[i] == alphas[j]):
- # set out Alpha flag to true
- blnAlpha = True
- # break our of the checkAlpha loop
- break
- j += 1
- # If char is an alpha, add it to our temporary string
- if (blnAlpha):
- strTmp += strInput[i]
- else:
- # If strTmp contains characters, or if at the end of the string
- if ((len(strTmp) > 0) or ((i + 1) >= len(strInput))):
- # Print the word
- print("Word = " + strTmp)
- # Print the left and right positions of the word
- print("Leftpos = " + str(leftpos))
- print("Rightpos = " + str(i))
- # Print the length of the word
- print("Length = " + str(len(strTmp)))
- # Update the leftpos value to whatever "i" is
- leftpos = i
- # Increment the word count by 1
- wordCount += 1
- # Reset the value of strTmp to an empty string
- strTmp = ""
- # Increment i by 1
- i += 1
- # Print the numbers of words found
- print("Found " + str(wordCount) + " words.")
- else:
- # Display the error message for no input
- print("Error: No input.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement