Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Substring index greater than string length
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cat8864




PostPosted: 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?

Turing:

var sentence := ""
var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z := 0

put "Please enter a sentence"
get sentence : *
put " "

var size := length (sentence)
for I : 1 .. size
    if index (sentence, "a" (I)) > 0 or index (sentence, "A" (I)) > 0 then
        a := a + 1
    elsif index (sentence, "b" (I)) > 0 or index (sentence, "B" (I)) > 0 then
        b := b + 1
    elsif index (sentence, "c" (I)) > 0 or index (sentence, "C" (I)) > 0 then
        c := c + 1
    elsif index (sentence, "d" (I)) > 0 or index (sentence, "D" (I)) > 0 then
        d := d + 1
    end if
end for
put "Here are the totals for each letter"
put "A = ", a
put "B = ", b
put "C = ", c
put "D = ", d
Sponsor
Sponsor
Sponsor
sponsor
drij




PostPosted: 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:
Turing:
if index (sentence (I), "a") > 0 or index (sentence (I), "A") > 0 then


EDIT: You don't actually have any need for the index function. It is much simpler and efficient to do the following:
Turing:
if sentence (I) = "a" or sentence (I) = "A" then
[Gandalf]




PostPosted: Mon Nov 24, 2008 7:36 pm   Post subject: RE:Substring index greater than string length

code:
"a" (I)

What does this evaluate to for different values of I?
Try it out:
code:
for i: 1 .. 3
    put i,  " ", "a"(i)
cat8864




PostPosted: Mon Nov 24, 2008 8:10 pm   Post subject: RE:Substring index greater than string length

Thanks
andrew.




PostPosted: 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:
code:
var letters : array 1..26 of int %This is basically 26 integers all under the same variable name, plus a number.

for i : 1..26 %This loop is to make all the letters equal 0
    letters (i) := 0
end for


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.
cat8864




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: