Computer Science Canada Substring index greater than string length |
Author: | cat8864 [ Mon Nov 24, 2008 7:02 pm ] | ||
Post subject: | Substring index greater than string length | ||
Why do I keep getting "Substring index is greater than length of string" when I have more than one character in my input?
|
Author: | drij [ Mon Nov 24, 2008 7:30 pm ] | ||||
Post subject: | Re: Substring index greater than string length | ||||
You have the string index on the wrong strings. It should look like this:
EDIT: You don't actually have any need for the index function. It is much simpler and efficient to do the following:
|
Author: | [Gandalf] [ Mon Nov 24, 2008 7:36 pm ] | ||||
Post subject: | RE:Substring index greater than string length | ||||
What does this evaluate to for different values of I? Try it out:
|
Author: | cat8864 [ Mon Nov 24, 2008 8:10 pm ] |
Post subject: | RE:Substring index greater than string length |
Thanks |
Author: | andrew. [ Mon Nov 24, 2008 8:15 pm ] | ||
Post subject: | RE:Substring index greater than string length | ||
Why did you make all the variables a to z? You could've made an array like this:
This is better because there is less code and also you can keep track of everything right in one variable. So let's say your sentence has 3 a's and 4 d's. Well you would add 3 to letters(1), because we are making 1=A, 2=B, etc. You would also add 4 to letters(4) because that is D. If you don't understand this and want to learn about it, check out the Turing Walkthrough in the Tutorials section. It explains arrays there perfectly. |
Author: | cat8864 [ Mon Nov 24, 2008 9:28 pm ] |
Post subject: | RE:Substring index greater than string length |
ya, I realized the length of the code was a bit of a problem so I changed it to ASCII characters in two for loops. Still needed to use the same letter selection though. Thanks everybody |