#Count a character in a string
import sys
#Variable Initialization
st = raw_input("Enter the string : ")
ch = raw_input("Which char to be counted ?")
#Checking
l = len(st)
count = 0
for i in st:
if i == ch:
count = count + 1
#Output
sys.stdout.write("\nThe character %c occurs %d times"%(ch,count))
#Counting vowels
import sys
#Variable Initialization
st = raw_input("Enter the sentence :")
count = 0
#Processing
for i in st:
if i == 'A'or i == 'E' or i == 'I' or i == 'O' or i == 'U' or i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
count = count + 1
#Result
sys.stdout.write("%d vowels are present in the sentence"%(count))
#Palindrome or not
import sys
#Variable Initialization
rst = ['' for i in range(0,20)]
st = raw_input("Enter the string : ")
j = len(st)-1
#Processing
rst = st[::-1]
#Result
if st == rst:
sys.stdout.write("%s is a palindrome string"%(st))
else:
sys.stdout.write("%s is not a palindrome string"%(st))
#String Concatenation
import sys
#Variable Initialization
st1 = raw_input("Enter first string : ")
st2 = raw_input("Enter second string : ")
#Processing
st = st1 + " " + st2
#Result
sys.stdout.write("\nResultant string is %s"%(st))
#Print greater string
import sys
#Variable Initialization
st1 = raw_input("Enter string 1 : ")
st2 = raw_input("Enter string 2 : ")
#Result
if st1 > st2:
sys.stdout.write("%s is alphabetically greater string"%(st1))
else:
sys.stdout.write("%s is alphabetically greater string"%(st2))
#String sorting
import sys
#Variable Initialization
names = ["" for i in range(0,50)]
n = int(raw_input("How many names ? "))
sys.stdout.write("\nEnter the %d names one by one"%(n))
for i in range(0,n):
names[i] = raw_input("")
for i in range(0,n):
for j in range(i+1,n):
if names[i] > names[j]:
temp = names[i]
names[i] = names[j]
names[j] = temp
#Result
sys.stdout.write("\nNames in alphabetical order")
for i in range(0,n):
sys.stdout.write("\n%s"%(names[i]))
#Lower case text to upper case
import sys
#Variable Initialization
st = raw_input("Enter a sentence : ")
#Result
st = st.upper()
sys.stdout.write("\nThe converted upper case string is \n%s"%(st))
#Determine the longest word
import sys
#Variable Initialization
st = raw_input("Enter a sentence ")
#Processing
lngst = max(st.split(), key=len)
#Result
sys.stdout.write("\nLongest word is %s"%(lngst))
sys.stdout.write("\nPress any key to continue...")
#Remove particular word in a text
import sys
#Variable Initialization
st = raw_input("Enter a sentence : ")
omit = raw_input("\nEnter word to omit : ")
word = st.split()
newst = ' '.join([i for i in word if i not in omit])
#Result
sys.stdout.write("\nAfter omitting the word %s\n%s"%(omit,newst))
sys.stdout.write("\nPress any key to continue...")
#Telegram expense calculation
import sys
#Variable Initialization
st = raw_input("Type the sentence for Telegram : ")
count = len(st.split())
if count <= 10:
amt = 5
else:
amt = 5 + (count - 10) * 1.25
#Result
sys.stdout.write("\nAmount to be paid for telegram = Rs. %6.2f"%(amt))
#Count lines, words and characters
import sys
#Variable Initialization
txt = raw_input("Enter the text, type $st end\n\n")
wds = 0
lns = 0
chs = 0
#Processing
for i in txt:
if i == ',' or i == '!' or i == '\t' or i == ' ':
wds += 1
chs += 1
else:
if i == '?' :
lns += 1
else:
if i == '.':
wds += 1
lns += 1
else:
chs += 1
#Result
sys.stdout.write("\n\nNumber of char(incl.blanks) = %d"%(chs))
sys.stdout.write("\nNumber of words = %d"%(wds))
sys.stdout.write("\nNumber of lines = %d"%(lns))