# The control string is just a string
format = "%s"
message = "hello"
print format % message
# 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
# 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
# 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
# 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_point = "hello"
string_var1 = string_point
print "%s\n%s"%(string_point,string_var1)
# 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)
# 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"
# 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