Qbasicnews.com

Full Version: Output backwards
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, i need help on a program that requires you to get a word or words from the user, and output the word(s) backwards...Any ideas?
well, an inefficient way off the top of my head

Code:
function reverse$ (text$)
  for x% = len(text$) to 1 step -1
    reversed$ = reversed$ + mid$(text$, x%, 1)
  next x%
  reverse$ = reversed$
end function

i'm sure there's a better, faster way using sadd but if you arent whipping out hundreds of these a second it shouldnt matter.
Yes, that would work, but the teacher hasn't taught about fuctions and stuff, and she will fail u for using commands she didnt teach...were at a grade 10 level (if that helps your thinking)
Just take off the function lines and it'll run once with the input provided in text$

Just for fun, I have a program that writes backwards as you type Big Grin
Code:
DEFINT A-Z
DECLARE FUNCTION dinputPRINT$ (x1, question$, acceptable$, limit, col)
CLS
allyup$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789 ?,."
moo$ = dinputPRINT(1, "Huh?", allyup$, 50, 15)

FUNCTION dinputPRINT$ (x1, question$, acceptable$, limit, col)

yy = CSRLIN 'yy = current y position

COLOR col: LOCATE yy, x1: PRINT question$
  x$ = ""
  newx = LEN(question$) + x1

DO

  i$ = INKEY$
  SELECT CASE i$
   CASE CHR$(8)
    IF LEN(x$) > 0 THEN

     x$ = RIGHT$(x$, LEN(x$) - 1)
     'x1 = newx + 1            
      
    'reprint the question
      LOCATE yy, x1: PRINT question$
    'print what has been typed
      LOCATE yy, x1 + newx
    PRINT x$ + SPACE$(limit - LEN(x$))
    END IF
   CASE CHR$(13)
    EXIT DO
   CASE CHR$(27)
    x$ = "99"
    EXIT DO
   CASE ELSE

    IF INSTR(acceptable$, i$) > 0 THEN
     IF LEN(x$) < limit THEN
      x$ = i$ + x$
    LOCATE yy, x1 + newx: PRINT x$
     END IF
    END IF
  END SELECT
  WAIT &H3DA, 8
LOOP
dinputPRINT$ = x$
END FUNCTION

Kinda longer than the previously posted code, huh? Tongue
This is a bit shorter than the other solution...and doesn't involve a function. It does us an array, though. There are lots of ways to do this...this is just with a minute's thought.




CLS
INPUT "input string"; a$ 'a$ is string

z = LEN(a$)
DIM temp(1 TO z) AS STRING * 1

FOR x = 1 TO z
temp((z + 1) - x) = MID$(a$, x, 1) 'transfer a$ in reverse order to array temp()
NEXT x

a$ = "" 'clear a$

FOR x = 1 TO z
a$ = a$ + temp(x) 'create new string with reverse order
NEXT x
PRINT a$
Here's another bit of code that will reverse the letters...note it uses integer division to avoid dealing with the middle byte for strings with odd numbers of bytes...



CLS
INPUT "input string"; word$

FOR x = 1 TO LEN(word$) \ 2
a$ = MID$(word$, x, 1)
b$ = MID$(word$, 1 + LEN(word$) - x, 1)
MID$(word$, x, 1) = b$
MID$(word$, 1 + LEN(word$) - x, 1) = a$
NEXT x
PRINT word$
END