Qbasicnews.com
April 08, 2020, 12:51:03 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Changing letters  (Read 6887 times)
Lanzaa
Forum Regular
**
Posts: 105



« on: February 01, 2003, 03:14:53 AM »

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? :-?
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #1 on: February 01, 2003, 03:26:34 AM »

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

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Lanzaa
Forum Regular
**
Posts: 105



« Reply #2 on: February 01, 2003, 12:00:32 PM »

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 }
Logged
Lanzaa
Forum Regular
**
Posts: 105



« Reply #3 on: February 02, 2003, 10:16:35 PM »

Well now i have an answer for one of the challenges. The encoding one look Shocked
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
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #4 on: February 02, 2003, 11:04:17 PM »

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!!
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Lanzaa
Forum Regular
**
Posts: 105



« Reply #5 on: February 03, 2003, 01:55:45 AM »

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
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #6 on: February 03, 2003, 07:22:10 AM »

okie dokie Smiley  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.
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Neo
Na_th_an
*****
Posts: 2150



« Reply #7 on: February 03, 2003, 08:04:37 AM »

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 Smiley
Logged
LooseCaboose
I hold this place together
*****
Posts: 981



« Reply #8 on: February 03, 2003, 06:02:09 PM »

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

esus saves.... Passes to Moses, shoots, he scores!
toonski84
__/--\__
*****
Posts: 2567



« Reply #9 on: February 03, 2003, 08:56:36 PM »

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

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Lanzaa
Forum Regular
**
Posts: 105



« Reply #10 on: February 03, 2003, 11:27:49 PM »

Well. I do understand you toonski with the mod and stuff but i have absolutly no idea about peek, poke, and sadd. I'll get into that later, but thanks all of you for helping.

and the new version of my 2nd code is up^ there

now to find some other project :humm:
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #11 on: February 04, 2003, 12:14:01 AM »

well, to break down the peek/poke/varseg/sadd method:

a string, like it infers, is a string of bytes.  each character in a string is 1 byte (value - 0 to 255).  now those strings are stored at an address segment, and an offset.  actually, it's just memory (which qb allocates for strings), but each address segment is a marker of 16 bytes, and each offset can go from zero to 65536.    think of it like a football field, where you have markers for 10 yards and individual yards.  so in essence you get access to a span of 1,048,576 in which your 640k of dos memory is stored, your bios (dont mess with it), your video memory, your text screen buffer, etc, essentially all the near memory you can play with without going to ems, xms or protected mode.

got it?  great.  now forget everything i just said, that's just background info ramble.  pretty much, every variable in qb has a segment and an offset that you can mess with directly.  to access these, you use:

def seg = segment
poke offset, byte_value
byte_value = peek (offset)

to find the segment and address of a qb variable, you say:

segment = varseg(variable)

and to find the offset (of the first point):

offset = varptr (numeric_variable)
offset = sadd(string_variable$)

so let's say i want to edit the first character of a string:

$mystring = "b guy walks into a bar"
def seg = varseg(mystring$)
poke sadd(mystring$) + 0, asc("A") 'zero is the character position - 1
print mystring$

should print, since you changed the first character to "A", "A guy walks into a bar"

to edit the second character, change the zero to 1, to edit the third, 2, etc.

to sum it up, this just pokes directly into your memory instead of using qb's string statements.  i hope i didnt confuse you by all that  :lol:
Logged

i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Neo
Na_th_an
*****
Posts: 2150



« Reply #12 on: February 04, 2003, 08:31:26 AM »

never mind Smiley it was just another possible way to do it lol
Logged
Lanzaa
Forum Regular
**
Posts: 105



« Reply #13 on: February 05, 2003, 01:01:08 PM »

Well I don't think I'm going to get into peek and stuff for a while so i made a very user friendly version of the encoder and decoder which changes "aaaaaaaaaa" into "bcdefghijk" and asks you if you want it decoded so here
Code:
start: CLS
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 GOTO start
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!