Today we are going to look at strings, for loops and if-then statements.
Play around with strings and figure out what the following commands do.
Notice the difference between the uses of := (assignment) and = (testing if two things
are equal). I used both od and end do (these are indications
of the end of a do loop). Similarly, fi and end if indicate the
end of an if statement. You will want to browse the help files on strings
so go to a help window and type string. The command with(StringTools);
loads a bunch of functions that are helpful for working with strings. I put it at the
top of the list of commands because you only need to execute it once to have those
functions.
> out:="";
> for n from 1 to length(A) do
out:=cat(out,A[n],B[n]);
end do:
> out;
> out:="";
> for n from 1 to length(A) do
out:=cat(out,A[-n],B[n]);
end do:
> out;
> out:="";
> for n from 1 to length(C) do
if not IsVowel(C[n]) then
out:=cat(out,C[n]);
else
out:=cat(out,".");
fi;
od;
> out;
Question 1:
Given some string, finish with the letters of the
string in reverse order. Example: if you were to start with the
string w:="Wow, thats amazing!",
then you should write commands which finish with the string "!gnizama staht ,woW"
Question 2:
Given a string with spaces in it like "Wow, thats amazing!", reverse
the order of the words in the string so that the end result is "amazing! thats Wow,".
Question 3:
Given a string with spaces in it like "Wow, thats amazing!", reverse
the order of the letters within the words in the string so that the end result is
",woW staht !gnizama".
You should open up a new worksheet and start from scratch. You will have to save
your work in a file and upload that file on to the course
moodle. Your
solution should be a sequence of commands where it is easy to change the input
string and after you execute the sequence of commands you should have the
correct output string. I don't need to see your experiments with the commands
that I have above, but I am interested in how far you are able to get with the
three questions above.