Chapter 8: String Manipulations in C

Example 1, Page number: SMC-4

In [1]:
#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))
Enter the string : MISSISSIPPI
Which char to be counted ?S

The character S occurs 4 times

Example 2, Page number: SMC-5

In [2]:
#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))
Enter the sentence :This is a book
5 vowels are present in the sentence

Example 3, Page number: SMC-6

In [3]:
#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))
Enter the string : HYDERABAD
HYDERABAD is not a palindrome string

Example 4, Page number: SMC-8

In [5]:
#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))
Enter first string : NEW
Enter second string : DELHI

Resultant string is NEW DELHI

Example 5, Page number: SMC-10

In [6]:
#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))
Enter string 1 : ALPHA
Enter string 2 : BETA
BETA is alphabetically greater string

Example 6, Page number: SMC-11

In [9]:
#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]))
How many names ? 4

Enter the 4 names one by oneDEEPAK
SHERIN
SONIKA
ARUN

Names in alphabetical order
ARUN
DEEPAK
SHERIN
SONIKA

Example 7, Page number: SMC-13

In [3]:
#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))
Enter a sentence : logical thinking is a must to learn programming

The converted upper case string is 
LOGICAL THINKING IS A MUST TO LEARN PROGRAMMING

Example 8, Page number: SMC-14

In [6]:
#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...")
Enter a sentence  SMALL THINGS MAKE PERFECTION BUT PERFECTION IS NO SMALL THING

Longest word is PERFECTION
Press any key to continue...

Example 9, Page number: SMC-16

In [9]:
#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...")
Enter a sentence : TO ACCESS THE NAME OF THE CITY IN THE LIST

Enter word to omit : THE

After omitting the word THE
TO ACCESS NAME OF CITY IN LIST
Press any key to continue...

Example 10, Page number: SMC-17

In [10]:
#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))
Type the sentence for Telegram : Congratulations on your success in Examinations.

Amount to be paid for telegram = Rs.   5.00

Example 11, Page number: SMC-19

In [5]:
#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))
            
Enter the text, type $st end

What is a string? How do you initialize it? Explain with example.$


Number of char(incl.blanks) = 63
Number of words = 12
Number of lines = 3