First off its not homework i only dream that im in a qbasic class right now, I'm 12, but i was wondering how to change and "a" to a "b" or "abc" to "bcd" and so on.
Any ideas? :-?
you'd need to extract each letter, and change them individually. you can use
mid$ for that. once you do that, you can use
asc to convert each number to its ascii value, add one, and revert to a string using mid$ again and the
chr$. i'm not going to tell you how to do it, that's for you to figure out, i've given you the tools. try it and come back if you have any problems. this was actually the first program i ever made, something to do exactly what you are, that i copied out of a code book and studied until i had every statement down.
30 min after 7:00am edited 15 times for gramar
I've done it!!!!!
Code:
CLS
INPUT "sentence test"; a$
FOR letter = 1 TO LEN(a$)
b = ASC(MID$(a$, letter, 1))
b = b + 1
b$ = CHR$(b)
MID$(a$, letter, 1) = b$
NEXT letter
PRINT a$
now i just need to stop spaces going! and z's going { or }
Well now i have an answer for one of the challenges. The encoding one look :o
Code:
CLS
start: INPUT "Encode or decode"; f$
g = 1
CLS
INPUT "Input sentence"; a$
CLS
PRINT a$
IF f$ = "decode" THEN g = -1
code: FOR letter = 1 TO LEN(a$)
leter = letter * g
b = ASC(MID$(a$, letter, 1))
b = b + leter
d$ = CHR$(b)
MID$(a$, letter, 1) = d$
NEXT letter
PRINT a$
PRINT
IF f$ = "encode" THEN INPUT "Do you want to decode? y / n "; d$
IF d$ = "y" THEN
CLS
LET f$ = ""
PRINT a$
g = -1
GOTO code
END IF
PRINT
INPUT "Again? y / n "; f$
IF f$ = "y" THEN
CLS
GOTO start
END IF
hehe tx
and anyone that can help me make ths smaller more efficient, ect. plz do
well, efficiency can mean many things, but for code clarity it isnt.
for instance, your code is very clear. but if you wanted to compress it:
b = ASC(MID$(a$, letter, 1))
b = b + letter
d$ = CHR$(b)
MID$(a$, letter, 1) = d$
could be:
MID$(a$, letter, 1) = chr$(ASC(MID$(a$, letter, 1)) + letter)
if you actually wanted to make it faster, well, that's a different story. string functions are difficult because qb handles them all, so the only way to increase the speed of such a program is to use
peek and
poke with
sadd, which can be VERY dangerous if you dont know what you are doing (as is anything involving peek and poke).
and while skipping spaces can be easily performed with an if...then statement, if you only want this to apply to a-z letters (and have z revert to a) then
Code:
function encode$ (message$)
for x = 1 to len(message$)
a = mid$(message$, x, 1)
select case a
case 65 to 90
a = (((a-65) + 1) mod 26) + 65
case 97 to 122
a = (((a-97) + 1) mod 26) + 97
end select
mid$(message$, x, 1) = chr$(a)
next x
encode$ = message$
end function
WARNING: THIS CODE IS UNTESTED AND PULLED OUT OF MY POSTERIOR. USE WITH CAUTION. NO MEMBERS OF QBASICNEWS CAN BE BLAMED IN THE EVENT THAT IT DOES NOT WORK!!
Well i changed both with that except i can't figure out in 5 min how to decode so im going to try tomarrow here are the codes :lol:
Code:
CLS
INPUT "sentence test"; a$
FOR letter = 1 TO LEN(a$)
b = ASC(MID$(a$, letter, 1))
SELECT CASE b
CASE 65 to 90
b = (((b - 65) + 1) MOD 26) + 65
CASE 97 to 122
b = (((b - 97) + 1) MOD 26) + 97
END SELECT
MID$(a$, letter, 1) = CHR$(b)
NEXT letter
PRINT a$
Code:
CLS
start: INPUT "Encode or decode"; f$
g = 0
CLS
INPUT "Input sentence"; a$
code: CLS
PRINT a$
FOR letter = 1 TO LEN(a$)
IF f$ = "decode" THEN number = (26 - (letter MOD 26))
IF f$ = "encode" THEN number = (letter MOD 26)
b = ASC(MID$(a$, letter, 1))
SELECT CASE b
CASE 65 TO 90
b = (((b - 65) + number) MOD 26) + 65
CASE 97 TO 122
b = (((b - 97) + number) MOD 26) + 97
END SELECT
MID$(a$, letter, 1) = CHR$(b)
NEXT letter
PRINT a$
PRINT
IF f$ = "encode" THEN INPUT "Do you want to decode? y / n "; f$
IF f$ = "y" THEN
f$ = "decode"
GOTO code
END IF
PRINT
INPUT "Again? y / n "; f$
IF f$ = "y" THEN
CLS
GOTO start
END IF
tx all
okie dokie
enjoy.
Code:
function decode$ (message$)
for x = 1 to len(message$)
a = mid$(message$, x, 1)
select case a
case 65 to 90
a = (((a-65) + 25) mod 26) + 65
case 97 to 122
a = (((a-97) + 25) mod 26) + 97
end select
mid$(message$, x, 1) = chr$(a)
next x
decode$ = message$
end function
hey, if you want to know how this works i'll break it down for you when i have some time later.
I think I have a more efficient piece of code.
Code:
MyText$="abc"
DEF SEG = VARSEG(MyText$)
FOR I = SADD(MyText$) TO SADD(MyText$) + LEN(MyText$) - 1
y% = PEEK(I)
IF y% = 255 THEN y% = -1
POKE I, y% + 1
NEXT I
DEF SEG
Hope you can do something with it
IMHO Toonski's code is much more effecient because it is understandable (and modifiable) at a glance, Neo's code may produce slightly more effecient machine code, but its meaning isnt instantly clear to a beginner and its not easily changed.
In terms of computer science, both Neo's and Toonski's algorithms are almost exactly the same speed (Toonski's takes 5 assignments and Neo's only 3) but both execute in O(1) constant time. The percieved speed difference on a modern computer, even for several hunderd iterations of either algorithm would be almost impossible to detect.
Would anyone be interested in reading some simple tutorials on algorithm complexity, ie discussions of the big-O notation, what makes certain algorithms faster than others and good selection of algorithms. I could write some Qbasic relevant tuts on the subject.
well, something as simple and quick as this is is difficult to compare complexity. peek and poke with sadd is definately faster, but it's hard to tell if our friend here will understand it. of course, there are some improvements to be made to neo's version, which is well done as a note, too.
y% = PEEK(I)
IF y% = 255 THEN y% = -1
POKE I, y% + 1
coud be
POKE I, (PEEK(I) + 1) AND 255
which is more efficient and compressed.
and i could be dimmed as an integer to boot. but like you said, i doubt with modern speeds it makes a difference.