Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # StringProcessing_Example.py
- # Author: Alex Paarfus <rapaarfus130@gmail.com>
- # Date: 2018-08-05
- #
- # Requirements:
- # -Prompt the user for a string input
- # -Assume multiple words
- # -Assume seperated by non-alphas
- # -Display the number of characters in the input (all)
- # -Use a While Loop to:
- # -Search for <space> characters in the String
- # -Extract the words between non-alpha seperators
- # -Display the length of each word
- # -Display each word
- # -Count the number of words in the string
- # -Condition: if len(word) >= 0 then {count++}
- # -Print the number of words in the string
- # -Cannot use split()
- #
- # Notes:
- # -Has some error handling built in, but not much
- # -I've only been writing python for like two days
- # -This is to help a friend learn, not save the world
- # -Some of this code was adapted from something I saw on StackOverflow
- # -I know, I know. Don't shoot me, though.
- # Functions
- # Check User input
- def __checkInput(string):
- # If the user hasn't entered anything, yell at them.
- if not string:
- print('Error, you didn\'t enter any text.')
- return False
- # If the user entered only a number, yell at them.
- if string.isnumeric():
- print('Error, numeric digits aren\'t actually words.')
- return False
- return True
- # Split, without split
- def __sws(string):
- # Declare some local variables
- i = 0
- # tmp and splitList will be used to split up the sentence.
- tmp = ''
- splitList = []
- # While i is between 0 and the highest index of string:
- while i < len(string):
- # if the current character isn't a space, concat tmp with the new character
- if not string[i].isspace():\
- tmp += string[i]
- # else, if the length of tmp is greater than 0, add the new word to splitList
- else:
- if len(tmp) > 0:
- splitList.append(tmp)
- # and, of course, reset tmp to its' init value
- tmp = ''
- # incrememnt i (index) by 1
- i += 1
- # code to handle the last word
- # if i is the length of the string, or more && if tmp contains data:
- if i >= len(string):
- if tmp:
- # append tmp to the list
- splitList.append(tmp)
- # Break out of the while loop
- break
- # Final check -- if the splitList array contains any data, return that.
- if splitList:
- return splitList
- # else, return None
- return None
- # Quick preface to the application:
- print('owo -- Enter a sentence and I\'ll show you:\n' + \
- '\t-The Total number of characters\n' + \
- '\t-The Total number of words\n' + \
- '\t-Each word\n' + \
- '\t-The length of each word\n\n')
- # Var Definitions
- # Prompt the user for input
- strInput = input('Ener a sentence: ')
- # If the user entered a real string of one or more words...
- if __checkInput(strInput):
- # Split the string into words only, without split()
- wordList = __sws(strInput)
- # If wordList contains *anything*, print out stats of input
- if wordList:
- # Display the total number of characters and words in the input
- print('Total number of characters: ' + str(len(strInput)))
- print('Total number of words: ' + str(len(wordList)))
- # Display each word and its' respective length
- for word in wordList:
- print('Word: ' + word)
- print('Length: ' + str(len(word)))
- # If wordList is empty, or unassigned; return this generic error.
- else:
- print('Error, something went wrong.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement