2009年6月24日 星期三

Homework 3 3/2 String process

1.
Write a program that can reverse the order of an input string.
For example,
if you input "ab", it will output "ba".
If you input "abcdefg", it should return "gfedcba".

程式畫面

輸入字串後,利用for迴圈,依序將字串內字元由尾端輸出在螢幕上。

2.
Write a program that can print names alternatively.
The names are in the format of "First Name + Last Name" or "Last Name, First Name". For example,
if you input "Walt Savitch", your program will output "Savitch, Walt".
If you input "Savitch, Walt", your program will output "Walt Savitch".

程式畫面

利用if判別式判定輸入為何種格式
若字串內有"," 則判定為"Last name, first name"格式

若字串內有" "且沒有","
則判定為"First name last name"格式

加上for迴圈使程式重複跑兩次,方便測試兩種格式的輸入與結果。

沒有留言:

張貼留言