String exercise.

Write the following Python functions within one program.

1.  CharCount(c, str) :  that receives a character c as a parameter and a string str as imput.  Have the function return the number of times that c occurs in the string.  For example CharCount(‘a’,”National Lampoon”) would return a 3 .

2. Reverse_Name(str): str is a string that looks like “Richard Simpson” , i.e. First name followed by space followed by last name.   Have this function break up this string and reassemble another one with the last name first following by a comma and space then the last name.  Reverse_Name(“Tom Sawyer”) would return “Sawyer, Tom”.

Have your main program be the following.

str = "Tom Sawyer"
str2 = "Danica McKellar"
print CharCount('a', str2)
print CharCount('e',str)
print CharCount('x', str)
print Reverse_Name("Tom Sawyer")
print Reverse_Name("Abraham Lincoln")

Print both the program and output on the same page.  Copy the output from the output window and past it after the source program and print.  Document the program with you name as comments in the source.  No output no grade.  Duplicate homeworks will get no credit.

Comments are closed.