Chapter 12, Strings and String Functions

Program 12-1, Page Number: 331

In [1]:
# The control string is just a string

format = "%s"
message = "hello"

print format % message
hello

Program 12-2, Page Number: 334

In [2]:
# The control string is just a string - part 2

format = "%.15s"
message = "hellohellohello"

# Read in the number of characters to be displayed from "message"
print "How many characters do you want displayed?",
size = 5
print size

# Convert size to 2-digit number in format[2] and format[3]

format = "%." + str(size / 10) + str(size % 10) + "s"
print "The format is %s"%format

# Print the string
print format % message
How many characters do you want displayed? 5
The format is %.05s
hello

Program 12-3, Page Number 336

In [3]:
# sprintf function 

# There is no sprintf functionality in python
# However an alternate way to solve the problem is...

print "Please enter a number:",
number = 17
print number

string = "The number is %d"%number

print "%s"%string

# OR 

print string
Please enter a number: 17
The number is 17
The number is 17

Program 12-4, Page Number 337

In [4]:
# Variable control string using the sprintf function

message = "hellohellohello"

# Read in the number of characters to be displayed from message 
print "How many characters do you want displayed?",
size = 7
print size

# Create format in "format" using "size"
format = "%%.%ds\n"%size
print "The format is: %s"%format

# Print the string
print format % message
How many characters do you want displayed? 7
The format is: %.7s

hellohe

Program 12-5, Page Number 338

In [5]:
# A terse string copy function

def strcpy(copy, original):
	""" copies one string into another """
	copy = original
	return copy

string1 = ""
string1 = strcpy(string1, "This is a message")
print "String 1 = %s"%string1

string2 = ""
string2 = strcpy(string2, string1)
print "String 2 = %s"%string2
String 1 = This is a message
String 2 = This is a message

Program 12-7, Page Number 344

In [7]:
string_point = "hello"

string_var1 = string_point

print "%s\n%s"%(string_point,string_var1)
hello
hello

Program 12-8, Page Number 346

In [8]:
# strcat function in python
# simple addition of strings results in concatenation in python

def strcat(string1, string2) :
	""" concatenates the string and returns the concatenated string """
	string2 = string1 + string2
	return string2

string1 = "hello"
string2 = strcat(string1," there")
print "string1 = %s\nstring2 = %s"%(string1,string2)
string1 = hello
string2 = hello there

Program 12-9, Page Number 348

In [9]:
# strcat version 2

def strcat(string1, string2) :
	string1 = string1 + string2
	string2 = string1
	return string1,string2

string1 = "hello"
string1,string2 = strcat(string1, " there")

print "string1 = %s\nstring2 = %s"%(string1,string2)

if string1 == string2 :
	print "string2 points to string1"
else :
	print "string2 does not point to string1"
string1 = hello there
string2 = hello there
string2 points to string1

Program 12-10, Page Number 351

In [10]:
# Alphabetizing program using dynamically allocated arrays

# Macro definition
MAX_ARRAY_SIZE = 20		# The array may have up to 20 elements
MAX_STRING_SIZE = 10 	# Each string may have up to 10 characters
# Message flags for "print_array"
ORIGINAL = 0
SORTED = 1

words = [ "cat", "pig", "dog", "earlobe", "bingo", "elbow", "likely", "radar" ]
array_size = 0

def get_size(size) :
	""" Read the size of the array into size (until a value is entered) """
	print "How many words? ",
	size = 8
	print size
	while size < 1 or size > MAX_ARRAY_SIZE :
		print "How many words? ",
		size = 8
		print size
	global array_size
	array_size = size

def get_array(array,size) :
	""" Read in the string array called 'array' of size 'size' """
	print "Please enter the words:"
	for i in array :
		print i
	
def print_array(array,size,message) :
	""" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) """
	printout = [ "\nThe original array is:", "\nThe sorted array is:", "\n" ]
	# Print the appropriate message
	print "%s"%printout[message]

	for i in array :
		print i

def pointer_sort(array,size) :
	""" sort string array """
	for i in range (size-1) :
		for j in range (i + 1, size) :
			if array[i] > array[j] :
				swap(i,j)

def swap(i, j) :
	""" Exchange strings """
	global words
	words[i],words[j] = words[j],words[i]

get_size(array_size)		# Read in the array size
get_array(words, array_size)	# Read in the string array "words" of size "array_size"
print_array(words, array_size, ORIGINAL)	# Echo the original array
pointer_sort(words, array_size)	
print_array(words, array_size, SORTED) # Print the sorted array
How many words?  8
Please enter the words:
cat
pig
dog
earlobe
bingo
elbow
likely
radar

The original array is:
cat
pig
dog
earlobe
bingo
elbow
likely
radar

The sorted array is:
bingo
cat
dog
earlobe
elbow
likely
pig
radar