Well, hi, I wanted to make a program, which turns normal words into a Caesar shift.

So...

HELLO

BECOMES;

JGNNW

That is with a shift of two.

I did not want to include IF statements in this, as that is 'lazy programming'

I have made a program which will convert one letter, but no more...

So... say, I typed A in, C would come out.

But, if I typed ABC in a really wierd symbol would come out...

The way that this works is by taking A, converting it into it's ASCII code (65) adding two (67) and then converting back into letter form...

So, a summary, I just want this code to be adapted so that it will convert more than one letter, I think I will need to use a loop, but I am unsure on how to implement this into my code, as I have not been doing programming for very long...

Program shift;
 
VAR
 
asciicode, number : Integer;
letter, asciiletter, newcode : Char;
 
Procedure input;
 
BEGIN
Writeln ('Enter a word');
readln(letter);
end;
 
Procedure convert;
 
BEGIN
asciicode := ord(letter);
asciiletter := chr(number);
newcode := chr(asciicode+2);
 
end;
 
Procedure output;
 
BEGIN
Writeln ('The ASCII code of ',letter,' is ',asciicode);
Writeln (newcode);
Readln;
end;
 
BEGIN
 
input;
convert;
output;
 
END.

You are on the right track, and yes you need a loop. However, whoever told you that using IF was lazy programming has no idea what they are talking about! Conditional execution is one of the cornerstones of programming. Example: what happens if you enter Y or Z? You go beyond the end of the ASCII uppercase alphabet. My idea is to cycle back to the start in that case so that ABCXYZ becomes CDEZAB and so on, and that requires use of IF.

Anyway, all you need is a loop to iterate over the characters in a string, convert them and put them back (looks like you need to read up on string use too)

procedure Convert(var s :string);
var
  i :integer;
begin
  for i := 1 to Length(s) do
    if Ord(s[i]) >= Ord('Y') then
      s[i] := Chr(Ord(s[i])-24)
    else
      s[i] := Chr(Ord(s[i])+2);
end;

Also note that you can use the result of Ord and Chr without storing them in intermediate variables.

*Head explodes* But you can just make it so that the number will go through a loop too... and I still know that there should be a way to do it without IF statements...

Oh... and I've been trying programming for about a year... but I am only 12...

*Head explodes* But you can just make it so that the number will go through a loop too... and I still know that there should be a way to do it without IF statements...

Oh... and I've been trying programming for about a year... but I am only 12...

You only need the IF statement in this case if you want to keep all of the output as all letters. If that doesn't matter, sure, you can just run the loop directly and eliminate the first part of the IF in my example but letters at the end of the alphabet will end up as the characters which come after the letters in ASCII.

However, I still question why you are so interested in avoiding IF. It is one of the most important statements in any programming language, and something you will need to use in every program you ever write. When you think about it, even your loop is a form of an IF statement ("if we're not done, repeat this loop") If you try to find a way to write all of your programs without using IF you are going to have a very hard time of it.

Are you perhaps confusing IF with GOTO? That statement (which isn't even available in some languages) is indeed considered bad programming and is best avoided except in special circumstances.

Anyway, good luck, hope this helps a little.

Well... I may want an IF statement, but I do not want it so that you go;

if a = B then
writeln ('D');

I want the coding for other program:
Write an applet class which construct a substitution alphabet to use as a cipher as follows: Use a keyword to construct the alphabet. For example, suppose the keyword is "zebra". Place this keyword at the beginning of the alphabet, and then fill in the other 21 slots with the remaining letters from the alphabet.

Ciper alphabet: zebracdfghijklmnopqstuvwxy

Plain alphabet: abcdefghijklmnopqrstuvwxyz

Now in the cipher text, the letter a is substituted by letter z, the letter b is substituted by letter e etc.

This class, called alphabet, should have a public method called cipher Alphabet() which is passed the keyword as a String and returns an alphabet string. +cipher Alphabet(String s): String.

I didn't know you could make applets using Pascal... Are you sure you aren't using Java or somesuch?

Either way, remember that your substitution cipher is composed of two separate pieces of information: the code word and the remaining alphabet: code word + remaining alphabet You can consider each part separately.

To create the remaining alphabet, just say the alphabet but leave out the letters found in the code word.

A thought: what happens if your code word has duplicates?

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.