Chapter 12 - Character, C-String, and C++ String Class Functions

example 12.1, page no. 262

In [1]:
ch = 'a'

while(ch != 'Q' and ch != 'q'):
    print "Press Q or q to quit, any other key to continue: ",
    ch = raw_input()
    if(ch != 'Q' and ch != 'q'):
        print "You want to continue? "
    else:
        print "You quit"
Press Q or q to quit, any other key to continue: r
 You want to continue? 
Press Q or q to quit, any other key to continue: q
 You quit

example 12.2, page no. 264

In [2]:
ch = 'a'

while(ch != 'Q' and ch != 'q'):
    print "Press Q or q to quit, any other key to continue: ",
    ch = raw_input()
    if(ch != 'Q' and ch != 'q'):
        print "You want to continue? "
    else:
        print "You quit"
Press Q or q to quit, any other key to continue: t
 You want to continue? 
Press Q or q to quit, any other key to continue: q
 You quit

example 12.3, page no. 266

In [3]:
ch = 'a'

while(ch != 'Q' and ch != 'q'):
    print "Press Q or q to quit, any other key to continue: ",
    ch = raw_input()
    if(ch != 'Q' and ch != 'q'):
        print "You want to continue? "
    else:
        print "You quit"
Press Q or q to quit, any other key to continue: q
 You quit

example 12.4, page no. 268

In [4]:
ch = 'a'

while(ch != 'Q' and ch != 'q'):
    print "Press Q or q to quit, any other key to continue: ",
    ch = raw_input()
    if(ch != 'Q' and ch != 'q'):
        print "You want to continue? "
    else:
        print "You quit"
Press Q or q to quit, any other key to continue: u
 You want to continue? 
Press Q or q to quit, any other key to continue: q
 You quit

example 12.5, page no. 269

In [5]:
name = []
print "Enter course number: ",
courseNum = int(raw_input())
print "Enter your name: ",
name = raw_input()
print "Course number is: ", courseNum
print "Your name is: ", name
Enter course number: 321
 Enter your name: Jeff
 Course number is:  321
Your name is:  Jeff

example 12.6, page no. 271

In [7]:
name = []
print "Enter course number: ",
courseNum = int(raw_input())
print "Enter your name: ",
name = raw_input()
print "Course number is: ", courseNum
print "Your name is: ", name
Enter course number: 222
 Enter your name: Jeff
 Course number is:  222
Your name is:  Jeff

example 12.7, page no. 273

In [8]:
ch = 'a'
while(ch != 'Q'):
    print "Press Q or q to quit, any other key to continue: ",
    ch = raw_input()
    ch = ch.upper()
    if(ch != 'Q'):
        print "You want to continue? "
    else:
        print "You quit"
Press Q or q to quit, any other key to continue: q
 You quit

example 12.8, page no. 280

In [9]:
print "Enter first string: ",
str1 = raw_input()
print "Enter second string: ",
str2 = raw_input()
if (str1 == str2):
    print "The two Cstrings are equal"
elif (str1 > str2):
    print "The first Cstring is larger"
else:
    print "The second Cstring is larger"
Enter first string: Jeff
 Enter second string: Kent
 The second Cstring is larger

example 12.9, page no. 281

In [1]:
import sys

print "Enter an integer: ",
input = raw_input()
for x in input:
    if (x == 0):
        if (not(x.isdigit()) and x != '-'):
            sys.exit()
    else:
        if(not(x.isdigit())):
            sys.exit()
num = int(input)
print num
Enter an integer: 567
 567