Computer Science Canada Handling Strings - Help |
Author: | globSter [ Wed Dec 07, 2005 9:09 pm ] |
Post subject: | Handling Strings - Help |
I'm just taking Turing for the first time in grade 10, and we've been given an assignment to create a program that is reponsible for returning a word entered backwards. As in, if the user was to enter: MAN the result would be: NAM however, if the user enters a palindrome such as WOW, then the output displays "This is a palindrome" on the screen. Now, I've worked with similar programming codes before, but I'm not quite sure if I'm on the right track. I pretty new to programming so this is probably a really basic question, but any help would be greatly appreciated. The Glob. |
Author: | Cervantes [ Wed Dec 07, 2005 9:30 pm ] | ||
Post subject: | Re: Handling Strings - Help | ||
globSter wrote: Now, I've worked with similar programming codes before, but I'm not quite sure if I'm on the right track. Post what you've got and/or post your algorithm. decreasing for loops might help you out here.
|
Author: | DIIST [ Wed Dec 07, 2005 9:30 pm ] |
Post subject: | |
You need to know how to do basic string manipulation to find the word entered backwords. Do do this, this tutorial might be of some help. http://www.compsci.ca/v2/viewtopic.php?t=8229 Once you know how to manipulate strings, the problem should seem a lot easier. |
Author: | globSter [ Wed Dec 07, 2005 10:06 pm ] | ||||
Post subject: | Similar Codes | ||||
These are some of the codes that I've worked with ... and will post another tomorrow.
Another program:
Thanks for the for decreasing loop suggestion, and I will try it. I have another program that I created using ord and chr codes. Will post tomorrow. |
Author: | Cervantes [ Thu Dec 08, 2005 2:03 pm ] | ||
Post subject: | Re: Similar Codes | ||
globSter wrote:
This is not quite correct. The total number of characters in the given words is length (line) - 10, since you added a character (space) for each word. |
Author: | globSter [ Thu Dec 08, 2005 7:31 pm ] |
Post subject: | Program |
Thankyou for the correction. I was able to solve my program problem. Thanks all for the help. |
Author: | Thuged_Out_G [ Fri Dec 09, 2005 11:44 pm ] | ||
Post subject: | |||
i really suck at string manipulation, and i was reading this topic ... figured id give it a try im sure there is a much better way to do this, but heres what i came up with
|
Author: | Cervantes [ Sat Dec 10, 2005 8:41 am ] | ||||||
Post subject: | |||||||
No need to store things in an array.
Two things, here. First, you only need to check up to floor(length (word) / 2). Second, the only way count could equal length (word) is if count has been incrimented each and every time. There's no point checking if count equals length (word) each time through your for loop because there is only one time in that for loop when they could possibly be equal. The value of count is not going to change, so you can easily move that if statement outside the for loop.
count and length (word) are fixed. There is no point comparing them each time through the loop. Move that if statement outside the loop to improve performance, and for better reasoned coding. Furthermore, if that condition was false at some point, you would miss letters from reverse. You wouldn't want that! Here's mine:
|