String searching functions 
Author Message
 String searching functions

The teacher of my programming methodolgy class insists that we use
QBASIC to code our solutions to an assignment, and although I know how
I want to approach solving it, I am not sure what functions in QBASIC
are available that will facilitate my implementation.  Here are my
*hopefully* simple questions:

First, I want to be able to search a string of characters for any
instance of another string, i.e. let's say I want to find out if the
string "and" occurs in the string "jduandhedegf."  Are there functions
that are available that will search the string automatically, or do I
need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
characters in order to accomplish this?

Second, is there a simple way of reversing a string of characters,
i.e. changing the string "abcd" to "dcba"?!

I appreciate the help.  Thank you!

Jason Bunting



Sun, 27 Jun 2004 04:33:00 GMT  
 String searching functions
On Tue, 8 Jan 2002 13:33:00 -0700, "Jason Bunting"

Quote:

>The teacher of my programming methodolgy class insists that we use
>QBASIC to code our solutions to an assignment, and although I know how
>I want to approach solving it, I am not sure what functions in QBASIC
>are available that will facilitate my implementation.  Here are my
>*hopefully* simple questions:

>First, I want to be able to search a string of characters for any
>instance of another string, i.e. let's say I want to find out if the
>string "and" occurs in the string "jduandhedegf."  Are there functions
>that are available that will search the string automatically, or do I
>need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
>characters in order to accomplish this?

>Second, is there a simple way of reversing a string of characters,
>i.e. changing the string "abcd" to "dcba"?!

>I appreciate the help.  Thank you!

>Jason Bunting

' 1. Look up the HELP on INSTR.
  CLS
  s$ = "hownowbrowncow"
  sub$ = "own"
  p% = INSTR(s$, sub$)
  PRINT p%  'first location of substring = 2
  p% = INSTR(p% + 1, s$, sub$)
  PRINT p%  'second location of substring = 9
  p% = INSTR(p% + 1, s$, sub$)
  PRINT p%  'substring not in string from this start = 0

' 2. The easy way is to concatenate the string in reverse order to
'    another string variable.
  s$ = "abcd"
  rev$ = ""   'empty string
  FOR ct% = 1 TO LEN(s$)
    rev$ = MID$(s$, ct%, 1) + rev$
  NEXT ct%
  PRINT s$; " reversed is "; rev$



Sun, 27 Jun 2004 05:33:37 GMT  
 String searching functions


Hello Jason,

Quote:
> The teacher of my programming methodolgy class insists that we use
> QBASIC to code our solutions to an assignment, and although I know how
> I want to approach solving it, I am not sure what functions in QBASIC
> are available that will facilitate my implementation.  Here are my
> *hopefully* simple questions:

> First, I want to be able to search a string of characters for any
> instance of another string, i.e. let's say I want to find out if the
> string "and" occurs in the string "jduandhedegf."  Are there functions
> that are available that will search the string automatically, or do I
> need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
> characters in order to accomplish this?

You would want to use the INSTR -function.

Quote:
> Second, is there a simple way of reversing a string of characters,
> i.e. changing the string "abcd" to "dcba"?!

Not by a single instruction.  I think you will have to create a Loop that
grabs every character of a string and *prepends* it to a Result-string.  Can
be done in five lines of code (one to initialize the result-string, three
for the loop & filling of the result-string, one to copy the result-string
back into the origional string).

Quote:
> I appreciate the help.  Thank you!

You're welcome.

Regards,
  Rudy Wieser



Sun, 27 Jun 2004 05:47:39 GMT  
 String searching functions


Quote:
> On Tue, 8 Jan 2002 13:33:00 -0700, "Jason Bunting"


<snip>

Quote:
> >First, I want to be able to search a string of characters for any
> >instance of another string, i.e. let's say I want to find out if the
> >string "and" occurs in the string "jduandhedegf."  Are there functions
> >that are available that will search the string automatically, or do I
> >need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
> >characters in order to accomplish this?

> >Second, is there a simple way of reversing a string of characters,
> >i.e. changing the string "abcd" to "dcba"?!

<snip>

Quote:
> ' 1. Look up the HELP on INSTR.

Thanks, I found out about that one just after I posted this . . . wouldn't
you know it?

Quote:
> ' 2. The easy way is to concatenate the string in reverse order to
> '    another string variable.
>   s$ = "abcd"
>   rev$ = ""   'empty string
>   FOR ct% = 1 TO LEN(s$)
>     rev$ = MID$(s$, ct%, 1) + rev$
>   NEXT ct%
>   PRINT s$; " reversed is "; rev$

Thank you again, this is beautiful.  I was thinking this was the way I would
have to accomplish the task, but I was hoping a function was extant for such
a purpose . . . Thanks to you, there is now!  Thank you for your time!

Jason Bunting



Sun, 27 Jun 2004 07:19:43 GMT  
 String searching functions
Rudy,

Quote:
> > The teacher of my programming methodolgy class insists that we use
> > QBASIC to code our solutions to an assignment, and although I know how
> > I want to approach solving it, I am not sure what functions in QBASIC
> > are available that will facilitate my implementation.  Here are my
> > *hopefully* simple questions:

> > First, I want to be able to search a string of characters for any
> > instance of another string, i.e. let's say I want to find out if the
> > string "and" occurs in the string "jduandhedegf."  Are there functions
> > that are available that will search the string automatically, or do I
> > need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
> > characters in order to accomplish this?
> You would want to use the INSTR -function.

Thank you!  I was perusing the QBASIC help system and found that one after I
had posted this question, but I appreciate your help nonetheless!

Quote:
> > Second, is there a simple way of reversing a string of characters,
> > i.e. changing the string "abcd" to "dcba"?!

> Not by a single instruction.  I think you will have to create a Loop that
> grabs every character of a string and *prepends* it to a Result-string.
Can
> be done in five lines of code (one to initialize the result-string, three
> for the loop & filling of the result-string, one to copy the result-string
> back into the origional string).

This was my fear, not because it would be necessarily difficult, but because
I know basic does not offer many string-manipulation functions.  Thank you
for the time and effort!

Jason Bunting



Sun, 27 Jun 2004 07:22:55 GMT  
 String searching functions


Quote:
> First, I want to be able to search a string of characters for any
> instance of another string, i.e. let's say I want to find out if the
> string "and" occurs in the string "jduandhedegf."  Are there functions
> that are available that will search the string automatically, or do I
> need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
> characters in order to accomplish this?

Yes, use INSTR():

INSTR("jduandhedegf.", "and")

returns 4 (MID$ position), i.e. exactly what you want. Note: 0 = no
occurence.

Quote:
> Second, is there a simple way of reversing a string of characters,
> i.e. changing the string "abcd" to "dcba"?!

There's no built-in function, but you can easily write

FUNCTION Reverse$(t$)
  h$ = ""
  FOR i% = 1 TO LEN(t$)
    h$ = MID$(t$, i%, 1) + h$
  NEXT i%
  Reverse$ = h$
END FUNCTION

=> PRINT Reverse$("abcd")

returns "dcba" as expected.

       Andreas



Mon, 28 Jun 2004 04:15:48 GMT  
 String searching functions


Quote:


> > First, I want to be able to search a string of characters for any
> > instance of another string, i.e. let's say I want to find out if the
> > string "and" occurs in the string "jduandhedegf."  Are there functions
> > that are available that will search the string automatically, or do I
> > need to use LEFT$, MID$, and RIGHT$ along with t{*filter*} non-matching
> > characters in order to accomplish this?

> Yes, use INSTR():

> INSTR("jduandhedegf.", "and")

> returns 4 (MID$ position), i.e. exactly what you want. Note: 0 = no
> occurence.

> > Second, is there a simple way of reversing a string of characters,
> > i.e. changing the string "abcd" to "dcba"?!

> There's no built-in function, but you can easily write

> FUNCTION Reverse$(t$)
>   h$ = ""
>   FOR i% = 1 TO LEN(t$)
>     h$ = MID$(t$, i%, 1) + h$
>   NEXT i%
>   Reverse$ = h$
> END FUNCTION

> => PRINT Reverse$("abcd")

> returns "dcba" as expected.

Thank you for the time you took answering my questions!

Jason Bunting



Mon, 28 Jun 2004 09:55:10 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. string search function

2. Function to search text file for given string

3. Integrate into Outlook's search and advanced search functions

4. Search a string and change a character if it exists in the string

5. Parse search string into query string?

6. How can I replace an arbitrary character in a string (String functions in general)

7. Executing Function using String Containing Function Name

8. Function call via string parameter in other function ?

9. How to invoke function when function name is in a string

10. Numeric search SLOWER than string search. Which Search?

11. string search for email addresses?

12. Character string search

 

 
Powered by phpBB® Forum Software