Simple string search in BASIC

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Simple string search in BASIC

Post by Shaun_B »

After speaking with one of the guys on IRC earlier today, I came up with this quick and dirty way of searching a string. Actually, this is a refined example. It could be optimised to speed it up a bit. Remember, though, that a BASIC string is only ever a maximum of 255 characters. Anyway, here's my example:

Code: Select all

10 rem simple search string example
20 dim r$(1): rem for reporting, messages below
30 r$(0)="no string found"
40 r$(1)="string found from index"
50 s$="abcdefghijklmnopqrstuvwxyz": rem string to search
60 i=1: rem index counter
70 a$="1": rem search condition
80 s=len(s$): rem length of string to search
90 a=len(a$): rem length of search condition
100 rem loop marker
110 if mid$(s$, i, a)<>a$ and i<s then i=i+1: goto 100
120 r=abs(mid$(s$, i, a)=a$): rem turns the true value into 1 for true and 0 for false, like C
130 print r$(r);:if r then print i
I hope this example is 'clean' and non-offensive enough for everyone.

At the moment, no string will be found: try changing the condition in line 70 to a$="x" or a$="def" for instance. It will search for exact patterns as specified. The number it returns will say where the string starts from, so if you search for z then the variable i should be 26, or if you search for y then it should be 25 - that's the same if you search for yz.

I'm sure there are better examples.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Prime

Re: Simple string search in BASIC

Post by Prime »

Bebz did this for me!
Can you post the c code to accompany this as it's alittle beyond me my friend(or php) :).
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Simple string search in BASIC

Post by Shaun_B »

I don't know if it'll be C but I'll be able to post pseudo-code that might look C-like... Just to get this out of the way, the opening brace starts on the same line, or on the next line? ;-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Simple string search in BASIC

Post by Shaun_B »

Caution! This is not tested C code so I don't know whether or not it works, but I think the logic is right based on my BASIC listing:

Code: Select all

// simple search string example
char report[1][];                                   // Declaration for reporting, messages below
char report[0][]    = "No string found";
char report[1][]    = "String found from index ";
char _str[]         = "abcdefghijklmnopqrstuvwxyz"; // String to search
int index           = 0;                            // Index counter
char _a[]           = "1";                         // Search condition
int main()
{
    int _str_len        = strlen(_str);          // Length of string to search
    int _a_len          = strlen(_a);            // Length of search condition
    int _result         = 0;                        // Zero is false
    while( strcmp(_str[index], _a[index]) && index<_str_len )
    {
        index++;
    }
    if( !strcmp(_str[index], _a[index]) )
    {
        _result = 1;                                // Turns the true value into 1 if true
    }
    printf("%s", report[_result][]);
    if( _result )
    {
        printf("%d", index);
    }
    return 0;
}
From what I recall, strcmp returns zero when true and 1 or more when false which is the usual reverse logic.

Anyway, I hope clears up the listing for you.

Regards,

Shaun.
Last edited by Shaun_B on Thu May 29, 2014 4:43 pm, edited 1 time in total.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Gonzo
Member
Member
Posts: 15
Joined: Sun Apr 13, 2014 12:59 pm
Contact:

Re: Simple string search in BASIC

Post by Gonzo »

I would change the initial BASIC code like this.

Code: Select all

80 a=len(a$): rem length of search condition
90 s=len(s$)-a+1:if s<1 then r=0:goto 120: rem max index of string to search
This avoids running loops through haystack when needle is longer than the remaining string. It also ends prematurely if needle is longer than haystack.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Simple string search in BASIC

Post by Shaun_B »

Gonzo wrote:I would change the initial BASIC code like this.

Code: Select all

80 a=len(a$): rem length of search condition
90 s=len(s$)-a+1:if s<1 then r=0:goto 120: rem max index of string to search
This avoids running loops through haystack when needle is longer than the remaining string. It also ends prematurely if needle is longer than haystack.
Without testing, I think goto 130 would work just as well as r has already been set in the condition.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 2 guests