You are invited to Log in or Register a free Frihost Account!

# I need some help with my algorithm

Denvis
This is my task: input a string, replace all "com" within string with "ab", print string

So this is the algorithm I made to do that. Can somebody desk check it for me?

 Code: BEGIN   x=1   input string : string = ""     REPEAT       IF mid(string,x,3) = "com" then         mid(string,x,3,) = "ab"         string = string + "ab"         mid(string,x,3) = "com"         x = x + 3       ELSE         string = string + mid(string,x,1)         x=x+1       ENDIF     UNTIL x = len(string-3)     PRINT STRING END

Essentially, what I'm doing is... I have an empty string (string = "") and i'm looking at the first 3 characters within that string (starting from 1 and not 0 even though it's treated as an array). If those first 3 characters equal to "com" then the "com" is replaced with "ab", the "ab" is added to the empty string becoming string = "ab" and then changing back the "ab" in the original string to "com". I change it back because, chances are there will be an error if I don't. Once I've changed it back x is added by 3. It's added by 3 because those 3 are equal to "com" and I can start looking at the next 3.

Now for the else. If those 3 characters does not equal to "com" i'm taking the first character of that string and placing it in the empty string and then adding 1 to x. It then compares the next 3, instead of 1,2,3 it's 2,3,4 etc so on... I think there's a problem here. Maybe not.

This happens until x (essentially considered as a 'counter') equals to the length of word (i'm unsure if the -3 is needed). The empty string (which in result will be filled) is then printed.

I haven't done a desk check and I'm worried it won't work. Any help will be much appreciated
infinisa
Hi Denvis

Looks like you posted this on the wrong forum. Try posting on the Software Forum.
BTW, you might mention the language you're using - is it BASIC?

Cheers
Denvis
 infinisa wrote: Hi Denvis Looks like you posted this on the wrong forum. Try posting on the Software Forum. BTW, you might mention the language you're using - is it BASIC? Cheers

Oh dear, I should of posted it elsewhere. I'll wait for it to be moved or ignored, since the I've already solved the problem

The algorithm isn't a language however it can be used in a language.

Wikipedia quote:
 Quote: Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.

 Code: BEGIN  x=1  input string : newstring = ""             REPEAT      IF mid(string,x,3) = "com" then        mid(string,x,3,) = "ab"        newstring = newstring + "ab"        mid(string,x,3) = "com"        x = x + 3      ELSE        newstring = newstring + mid(string,x,1)        x=x+1      ENDIF    UNTIL x >= len(string-2)     PRINT newSTRING END