Chapter 8: C- Strings

Example 8.1, page no. 184

In [1]:
n= [44] # n holds the int 44
print "int n=44; // n holds the int 44:\n";
print "\t\t n = " , n 
print "\t\t &n = " , hex(id(n))
pn = n 
print "int* pn=&n; // pn holds the address of n:\n";
print "\t\t n = " , n 
print "\t\t &n = " , hex(id(n))
print "\t\t pn = " , hex(id(pn)) 
print "\t\t &pn = " , hex(id(hex(id(pn))))
print "\t\t *pn = " , pn

pn[0] = 77 # changes the value of n to 77
print "*pn = 77; // changes the value of n to 77:\n";
print "\t\t n = " , n 
print "\t\t &n = " , hex(id(n))
print "\t\t pn = " , hex(id(pn)) 
print "\t\t &pn = " , hex(id(hex(id(pn))))
print "\t\t *pn = " , pn

q = n 
print "int* q=&n; // q also holds the address of n:\n";
print "\t\t n = " , n 
print "\t\t &n = " , hex(id(n))
print "\t\t pn = " , hex(id(pn)) 
print "\t\t &pn = " , hex(id(hex(id(pn))))
print "\t\t *pn = " , pn
print "\t\t q = " , hex(id(q))
print "\t\t &q = " , hex(id(hex(id(hex(id(pn))))))
print "\t\t *q = " , q 
int n=44; // n holds the int 44:

		 n =  [44]
		 &n =  0x9bfb92c
int* pn=&n; // pn holds the address of n:

		 n =  [44]
		 &n =  0x9bfb92c
		 pn =  0x9bfb92c
		 &pn =  0x9bf5aa0
		 *pn =  [44]
*pn = 77; // changes the value of n to 77:

		 n =  [77]
		 &n =  0x9bfb92c
		 pn =  0x9bfb92c
		 &pn =  0x9c6a760
		 *pn =  [77]
int* q=&n; // q also holds the address of n:

		 n =  [77]
		 &n =  0x9bfb92c
		 pn =  0x9bfb92c
		 &pn =  0x9bf5c80
		 *pn =  [77]
		 q =  0x9bfb92c
		 &q =  0x9c6a760
		 *q =  [77]

Example 8.2, page no. 185

In [2]:
s = "ABCD"
for i in range(4):
    print "s[" , i , "] = '" , s[i] , "'\n";
s[ 0 ] = ' A '

s[ 1 ] = ' B '

s[ 2 ] = ' C '

s[ 3 ] = ' D '

Example 8.3, page no. 186

In [6]:
while True:
    word = raw_input()
    if len(word) < 2:
        break
    l = word.split(' ')
    for i in l:
        print '\t"' , i , '"'
Today's date is March 12, 2000.
	" Today's "
	" date "
	" is "
	" March "
	" 12, "
	" 2000. "
Tomorrow is Monday.
	" Tomorrow "
	" is "
	" Monday. "

Example 8.4, page no. 187

In [7]:
while True:
    line = raw_input()
    if len(line) < 2:
        break
    print "\t[" , line , "]"
Once upon a midnight dreary, while I pondered, weak and weary,
	[ Once upon a midnight dreary, while I pondered, weak and weary, ]
Over a many quaint and curious volume of forgotten lore,
	[ Over a many quaint and curious volume of forgotten lore, ]

Example 8.5, page no. 187

In [8]:
while True:
    word = raw_input()
    if len(word) < 2:
        break
    l = word.split(',')
    for i in range(len(l)-1):
        print '\t[' , l[i] , ']'
Once upon a midnight dreary, while I pondered, weak and weary,
	[ Once upon a midnight dreary ]
	[  while I pondered ]
	[  weak and weary ]
Over a many quaint and curious volume of forgotten lore,
	[ Over a many quaint and curious volume of forgotten lore ]

Example 8.6, page no. 188

In [9]:
count = 0
while True:
    a = raw_input()
    if len(a) < 1:
        break
    for ch in a:
        if (ch == 'e'): count+=1
        
print count , " e's were counted.\n"
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore,

11  e's were counted.

Example 8.7, page no. 188

In [10]:
while True:
    a = raw_input()
    if len(a) < 1:
        break
    print a.title()
Fourscore and seven years ago our fathers
Fourscore And Seven Years Ago Our Fathers
brought forth upon this continent a new nation,
Brought Forth Upon This Continent A New Nation,

Example 8.8, page no. 189

In [11]:
a = raw_input()
l = a.split(' ')
nos = []
for i in l:
    try:
        i = int(i)
        nos.append(i)
    except:
        continue
m = nos[0]
n = nos[1]    
print m , " + " , n , " = " , m+n
what is 305 plus 9416 ?
305  +  9416  =  9721

Example 8.10, page no. 191

In [12]:
count=0

print "Enter at most 4 names with at most 19 characters:\n";
while (True):
    n = raw_input()
    if len(n) < 1:
        break
    name.append(n)
    count += 1
    
print "The names are:\n"
for i in range(count):
    print "\t" , i , ". [" , name[i] , "]" 
Enter at most 4 names with at most 19 characters:

George Washington
John Adams
Thomas Jefferson

The names are:

	0 . [ George Washington ]
	1 . [ John Adams ]
	2 . [ Thomas Jefferson ]

Example 8.11, page no. 192

In [13]:
name = []
count=0

print "Enter at most 4 names with at most 19 characters:\n";
while (True):
    n = raw_input()
    if len(n) < 1:
        break
    name.append(n)
    count += 1
    
print "The names are:\n"
for i in range(count):
    print "\t" , i , ". [" , name[i] , "]" 
Enter at most 4 names with at most 19 characters:

George Washington
John Adams
Thomas Jefferson

The names are:

	0 . [ George Washington ]
	1 . [ John Adams ]
	2 . [ Thomas Jefferson ]

Example 8.12, page no. 193

In [14]:
name = [ "George Washington", "John Adams", "Thomas Jefferson"]
print "The names are:\n"
for i in range(3):
    print  "\t" , i , ". [" , name[i] , "]"
The names are:

	0 . [ George Washington ]
	1 . [ John Adams ]
	2 . [ Thomas Jefferson ]

Example 8.13, page no. 193

In [15]:
s = "ABCDEFG"
print "len(" , s , ") = " , len(s) 
print "len(\"\") = " , len("")
print "Enter string: "
b = raw_input()
print "len(" , b , ") = " , len(b)
len( ABCDEFG ) =  7
len("") =  0
Enter string: 
hello how are you !!!
len( hello how are you !!! ) =  21

Example 8.14, page no. 194

In [16]:
s = "The Mississippi is a long river."
print 's = "' , s , '"'
p = s.find(' ')
print "find(s, ' ') points to s[" , p , "]."
p = s.find('s')
print "find(s, 's') points to s[" , p , "]."
p = s.rfind('s')
print "reverse find(s, 's') points to s[" , p , "]."
p = s.find("is")
print "strstr(s, \"is\") points to s[" , p , "]."
p = s.find("isi")
if p== -1:
    print 's.find("isi") returns NULL'
s = " The Mississippi is a long river. "
find(s, ' ') points to s[ 3 ].
find(s, 's') points to s[ 6 ].
reverse find(s, 's') points to s[ 17 ].
strstr(s, "is") points to s[ 5 ].
s.find("isi") returns NULL

Example 8.15, page no. 195

In [17]:
s1 = "ABCDEFG"
s2 = "XYZ" 
print "Before strcpy(s1,s2):\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
s1 = s2
print "After strcpy(s1,s2):\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
Before strcpy(s1,s2):

	s1 = [ ABCDEFG ], length =  7
	s2 = [ XYZ ], length =  3
After strcpy(s1,s2):

	s1 = [ XYZ ], length =  3
	s2 = [ XYZ ], length =  3

Example 8.16, page no. 195

In [18]:
s1 = "ABCDEFG"
s2 = "XYZ" 
print "Before strcpy(s1,s2,2):\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
s1 = s2[:2] + s1[2:]
print "After strcpy(s1,s2,2):\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
Before strcpy(s1,s2,2):

	s1 = [ ABCDEFG ], length =  7
	s2 = [ XYZ ], length =  3
After strcpy(s1,s2,2):

	s1 = [ XYCDEFG ], length =  7
	s2 = [ XYZ ], length =  3

Example 8.17, page no. 196

In [19]:
s1 = "ABCDEFG"
s2 = "XYZ" 
print "Before string concatination :\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
s1 += s2
print "After string concatination :" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
Before string concatination :

	s1 = [ ABCDEFG ], length =  7
	s2 = [ XYZ ], length =  3
After string concatination :
	s1 = [ ABCDEFGXYZ ], length =  10
	s2 = [ XYZ ], length =  3

Example 8.18, page no. 197

In [20]:
s1 = "ABCDEFG"
s2 = "XYZ" 
print "Before string concatination :\n" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
s1 += s2[:2]
print "After string concatination :" 
print "\ts1 = [" , s1 , "], length = " , len(s1)   
print "\ts2 = [" , s2 , "], length = " , len(s2)   
Before string concatination :

	s1 = [ ABCDEFG ], length =  7
	s2 = [ XYZ ], length =  3
After string concatination :
	s1 = [ ABCDEFGXY ], length =  9
	s2 = [ XYZ ], length =  3

Example 8.19, page no. 197

In [21]:
s = "Today's date is March 12, 2000."

print "The string is: [" , s , "] \nIts tokens are: "
p = s.split(" ")

for i in p:
    print "\t[" , i , "] "

print "Now the string is: [" , p[0] , "] ";
The string is: [ Today's date is March 12, 2000. ] 
Its tokens are: 
	[ Today's ] 
	[ date ] 
	[ is ] 
	[ March ] 
	[ 12, ] 
	[ 2000. ] 
Now the string is: [ Today's ] 

Example 8.20, page no. 199

In [22]:
def strpbrk(s,s1):
    found = []
    for i in range(len(s1)):
        if s1[i] in s:
            index = s.find(s1[i])
            found.append(index)
    if found:
        return min(found)
    return None
            

s = "The Mississippi is a long river."
print 's = "' , s , '"'
p = strpbrk(s, "nopqr")
print 'strpbrk(s, "nopqr") points to s[' , p , "]."
p = strpbrk(s, "NOPQR")
if (p == None):
    print 'strpbrk(s, "NOPQR") returns NULL.\n'
s = " The Mississippi is a long river. "
strpbrk(s, "nopqr") points to s[ 12 ].
strpbrk(s, "NOPQR") returns NULL.