Chapter 9: Puppetting On Strings

Print String , Page number: 329

In [1]:
#Variable declaration
name= "Klinsman" # character array or string
i = 0

#while loop for printing
while i <= 7 :
    print name[i] 
    i = i + 1
K
l
i
n
s
m
a
n

Print String, Page number: 330

In [2]:
#Variable declaration
name= "Klinsman\0" # character array or string
i = 0

#while loop for printing
while (name[i] != '\0') :
    print  name[i]
    i = i + 1
K
l
i
n
s
m
a
n

Print String Using Pointer, Page number: 330

In [3]:
#Variable declaration
name = "Klinsman\0" # string or character array
i = 0
ptr = name[i] # store base address of string

#Result
while ptr != '\0':
    print ptr
    i = i+1
    ptr = name[i]
K
l
i
n
s
m
a
n

strlen Function, Page number: 336

In [4]:
#Variable declaration
arr = "Bamboozled"  # character array or string

#string length function
len1 = len(arr) 
len2 = len( "Humpty Dumpty" )

#Result
print "string = %s length = %d" %( arr, len1 )
print "string = %s length = %d" %("Humpty Dumpty", len2 )
string = Bamboozled length = 10
string = Humpty Dumpty length = 13

xstrlen Function, Page number: 337

In [5]:
#Function definition
def xstrlen(s):
    length = 0
    while ( s[length] != '\0' ):
        length  += 1
    return length 

#Variable declaration
arr = "Bamboozled\0"  # character array or string

#Function calls
len1 = xstrlen(arr) 
len2 = xstrlen( "Humpty Dumpty\0" )

#Result
print "string = %s length = %d" %( arr, len1 )
print "string = %s length = %d" %("Humpty Dumpty\0", len2 )
string = Bamboozled length = 10
string = Humpty Dumpty length = 13

strcpy Function, Page number: 338

In [6]:
#Variable declaration
source = "Sayonara" 
target = [] 

#strcpy function
import copy
target = copy.copy(source)

#Result
print "source string = ", source  
print "target string = ", target 
source string =  Sayonara
target string =  Sayonara

xstrcpy Function, Page number: 339

In [7]:
#Function definition
def xstrcpy (t,s):
    i = 0
    while ( s[i] != '\0' ):
        t = t + s[i]
        i = i + 1
    return t
    
#Variable declaration
source = "Sayonara\0" 
target = ''

target = xstrcpy ( target, source ) # function call

#Result
print "source string = ", source  
print "target string = ", target 
source string =  Sayonara
target string =  Sayonara

strcat Function, Page number: 342

In [8]:
#Variable declaration
source = "Folks!" 
target = "Hello"
 
target = target + source # string concatenation
#Result
print  "source string = ", source 
print  "target string = ", target 
source string =  Folks!
target string =  HelloFolks!

strcmp Function, Page number: 343

In [9]:
#Variable declaration
string1 = "Jerry\0" 
string2 = "Ferry\0"

#Function definition
def strcmp (string1 , string2):
    if (string1 == string2):
        v = 0 #If the two strings are identical, strcmp returns a value zero
        return v
    else:
        n = 0
        while ( string1[n]):
            if ( string1[n] == string2[n]):
                n = n + 1
                continue
            else:
                v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters
                return v
    return v

#Function call
i = strcmp ( string1, "Jerry\0" ) 
j = strcmp ( string1, string2 ) 
k = strcmp ( string1, "Jerry boy\0" )

#Result
print i,j,k
0 4 -32

Two Dimensional Array of Characters, Page number: 344

In [10]:
#Function definition
def strcmp (string1 , string2):
    if (string1 == string2):
        v = 0 #If the two strings are identical, strcmp returns a value zero
        return v
    else:
        n = 0
        while ( string1[n]):
            if ( string1[n] == string2[n]):
                n = n + 1
                continue
            else:
                v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters
                return v
    return v

#Variable declaration
FOUND = 1
NOTFOUND = 0
masterlist =["akshay","parag","raman","srinivas","gopal","rajesh"]
yourname = []
flag = NOTFOUND

#Input from user
#yourname = raw_input("Enter your name: ")
yourname = "Akshatha"

#Checking in the master list
for i in range(0,6):
    a = strcmp ( masterlist[i], yourname )
    if a == 0:
        print "Welcome, you can enter the palace" 
        flag = FOUND
        break
    
if flag == NOTFOUND :
    print  "Sorry, you are a trespasser" 
Sorry, you are a trespasser

Exchange Names using 2-D Array of Characters, Page number: 348

In [11]:
#Variable declaration
names = ["akshay\0\0\0","parag\0\0\0\0","raman\0\0\0\0","srinivas\0","gopal\0\0\0\0","rajesh\0\0\0"]

#Initial condition
print "Original: ", names[2], names[3] 

#Exchanging names
for i in range(0,9):
    t = names[3][i]
    names[3] = names[3][0:i] + names[2][i] + names[3][i+1:]
    names[2] = names[2][0:i] + t + names[2][i+1:]
    

#Result   
print  "New: ", names[2], names[3] 
Original:  raman srinivas
New:  srinivas raman

Exchange Names using Array of Pointers to Strings, Page number: 349

In [12]:
#Variable declaration
names = ["akshay\0\0\0","parag\0\0\0\0","raman\0\0\0\0","srinivas\0","gopal\0\0\0\0","rajesh\0\0\0"]

#Initial condition
print "Original: ", names[2], names[3] 

#Exchanging names
temp = names[2] 
names[2] = names[3] 
names[3] = temp 

#Result   
print  "New: ", names[2], names[3]  
Original:  raman srinivas
New:  srinivas raman

Solution, Page number: 351

In [15]:
import copy

#Variable declaration
names = []
n=["John","Max","Jim","Tony","Tom","Harry"]

for i in range(0,6):
    n1 = n[i]
    p = copy.copy(n1)
    names.append(p) 

for i in range(0,6):
    print names[i] 
John
Max
Jim
Tony
Tom
Harry
In [ ]: