# String manipulation
"""
Implementing programs using Strings. (reverse, palindrome, character count, replacing characters,
remove characters)
"""
def fnReverse(s):
return s[::-1]
def fnPalindrome(s):
if(s==s[::-1]):
return "String is a palindrome"
else:
return "String is not a palindrome"
def fnCharcount(s):
charcount = 0
for ch in s:
if(ch.isalpha()):
charcount+=1
return charcount
def fnWordCount(s):
L=s.split()
return len(L)
def fnReplace(s,ss,rs):
if(ss in s):
return s.replace(ss,rs)
else:
return "Search string not found. Cannot replace"
def fnRemove(s, rs):
if(rs in s):
return s.replace(rs,"")
else:
return "String not found. Cannot remove"
S = input("Enter string : ")
print("Reverse of the string : ",fnReverse(S))
print("Palindrome check : ")
print(fnPalindrome(S))
print("Character count =",fnCharcount(S))
print("Word count =",fnWordCount(S))
searchstr = input("Enter search string : ")
replacestr = input("Enter string to replace : ")
print("Replaced string :")
print(fnReplace(S,searchstr,replacestr))
removestr = input("Enter string to remove : ")
print("Remove string :")
print(fnRemove(S,removestr))
"""
Sample output
>python 7_stringmanip.py
Enter string : HELLO IT
Reverse of the string : TI OLLEH
Palindrome check :
String is not a palindrome
Character count = 7
Word count = 2
Enter search string : IT
Enter string to replace : CSE
Replaced string :
HELLO CSE
Enter string to remove : IT
Remove string :
HELLO
"""
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu