2016-11-26 9 views
1

入力テキストには、各単語の最初の文字を大文字にし、逆順でテキストを大文字にする必要はありません。私は母音の数を調べることができましたが、他の2人とは苦労しました。リバース機能を持たないリバース文字列

def main(): 

    vowelCount = 0 
    text = 'abc' 

    while(len(text) != 0): 
     text = input('Please etner some text or press <ENTER> to exit: ') 

     for char in text: 
      if char in 'aeiouAEIOU': 
       vowelCount += 1 
     print('Vowels:', vowelCount) 
     vowelCount = 0 



     for i in text: 
      i = text.find('') + 1 
     print(i) 

     print(text[0].upper() + text[1:]) 


main() 
+0

リバースまたは逆のすべての文字の単語? – AChampion

+6

[文字列の逆転は既に詳細に説明されています。](http://stackoverflow.com/q/931092/364696) – ShadowRanger

答えて

3

ここでは、文字列を反転する2つの例を示します。 文字列をスライスします。

>>> s = 'hello' 
>>> reversed_s = s[::-1] 

ループを使用します。

res = '' 
for char in s: 
    res = char + res 

完全なコード

def main(): 
    # Run infinitely until break or return 
    # it's more elegant to do a while loop this way with a condition to 
    # break instead of setting an initial variable with random value. 
    while True: 
     text = input('Please enter some text or press <ENTER> to exit: ') 
     # if nothing is entered then break 
     if not text: 
      break 

     vowelCount = 0 
     res = '' 
     prev_letter = None 

     for char in text: 
      if char.lower() in 'aeiou': 
       vowelCount += 1 
      # If previous letter was a space or it is the first letter 
      # then capitalise it. 
      if prev_letter == ' ' or prev_letter is None: 
       char = char.upper() 

      res += char # add char to result string 
      prev_letter = char # update prev_letter 

     print(res) # capitalised string 
     print(res[::-1]) # reverse the string 
     print('Vowel Count is: {0}'.format(vowelCount)) 

# Example 
Please enter some text or press <ENTER> to exit: hello world! 
Hello World! 
!dlroW olleH 
Vowel Count is: 3 
+0

チップマンに感謝しますが、文字列を逆にしないでループを使用して各単語を大文字にします。 – MrG

+0

@MrG更新された回答。私はそれを分割して、結果の文字列が逆に構築されず、最初に提案された方法を使用して末尾を反転させます(スライス) –

関連する問題