Advertisement
Stewie410

StringProcessing_Example.py

Aug 5th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. # StringProcessing_Example.py
  2. # Author:       Alex Paarfus <rapaarfus130@gmail.com>
  3. # Date:         2018-08-05
  4. #
  5. # Requirements:
  6. #       -Prompt the user for a string input
  7. #           -Assume multiple words
  8. #           -Assume seperated by non-alphas
  9. #       -Display the number of characters in the input (all)
  10. #       -Use a While Loop to:
  11. #           -Search for <space> characters in the String
  12. #           -Extract the words between non-alpha seperators
  13. #           -Display the length of each word
  14. #           -Display each word
  15. #           -Count the number of words in the string
  16. #               -Condition: if len(word) >= 0 then {count++}
  17. #       -Print the number of words in the string
  18. #       -Cannot use split()
  19. #
  20. # Notes:
  21. #       -Has some error handling built in, but not much
  22. #       -I've only been writing python for like two days
  23. #       -This is to help a friend learn, not save the world
  24. #       -Some of this code was adapted from something I saw on StackOverflow
  25. #           -I know, I know.  Don't shoot me, though.
  26.  
  27. # Functions
  28. # Check User input
  29. def __checkInput(string):
  30.     # If the user hasn't entered anything, yell at them.
  31.     if not string:
  32.         print('Error, you didn\'t enter any text.')
  33.         return False
  34.     # If the user entered only a number, yell at them.
  35.     if string.isnumeric():
  36.         print('Error, numeric digits aren\'t actually words.')
  37.         return False
  38.     return True
  39.  
  40. # Split, without split
  41. def __sws(string):
  42.     # Declare some local variables
  43.     i = 0
  44.     # tmp and splitList will be used to split up the sentence.
  45.     tmp = ''
  46.     splitList = []
  47.     # While i is between 0 and the highest index of string:
  48.     while i < len(string):
  49.         # if the current character isn't a space, concat tmp with the new character
  50.         if not string[i].isspace():\
  51.             tmp += string[i]
  52.         # else, if the length of tmp is greater than 0, add the new word to splitList
  53.         else:
  54.             if len(tmp) > 0:
  55.                 splitList.append(tmp)
  56.                 # and, of course, reset tmp to its' init value
  57.                 tmp = ''
  58.         # incrememnt i (index) by 1
  59.         i += 1
  60.         # code to handle the last word
  61.         # if i is the length of the string, or more && if tmp contains data:
  62.         if i >= len(string):
  63.             if tmp:
  64.                 # append tmp to the list
  65.                 splitList.append(tmp)
  66.             # Break out of the while loop
  67.             break
  68.     # Final check -- if the splitList array contains any data, return that.
  69.     if splitList:
  70.         return splitList
  71.     # else, return None
  72.     return None      
  73.  
  74. # Quick preface to the application:
  75. print('owo -- Enter a sentence and I\'ll show you:\n' + \
  76.     '\t-The Total number of characters\n' + \
  77.     '\t-The Total number of words\n' + \
  78.     '\t-Each word\n' + \
  79.     '\t-The length of each word\n\n')
  80.        
  81. # Var Definitions
  82. # Prompt the user for input
  83. strInput = input('Ener a sentence: ')
  84.  
  85. # If the user entered a real string of one or more words...
  86. if __checkInput(strInput):
  87.     # Split the string into words only, without split()
  88.     wordList = __sws(strInput)
  89.     # If wordList contains *anything*, print out stats of input
  90.     if wordList:
  91.         # Display the total number of characters and words in the input
  92.         print('Total number of characters: ' + str(len(strInput)))
  93.         print('Total number of words:      ' + str(len(wordList)))
  94.         # Display each word and its' respective length
  95.         for word in wordList:
  96.             print('Word:    ' + word)
  97.             print('Length:  ' + str(len(word)))
  98.     # If wordList is empty, or unassigned; return this generic error.
  99.     else:
  100.         print('Error, something went wrong.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement