Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String class
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Mon Dec 11, 2006 8:51 pm   Post subject: String class

got bored, so I made a string class, heres the source:

Turing:

class String
    import Str
    export insert, content, remove, strip, size, make_upper, make_lower, gets, make_original, as_lower, as_upper, as_stripped, originally

    var value : string := ""
    var original : string := ""

    fcn content : string
        result value
    end content

    procedure insert (letter : string (1), position : int)
        var oldword := value
        value := oldword (1 .. position - 1) + letter + oldword (position .. *)
    end insert

    procedure remove (position : int)
        if original = "" then
            original := value
        end if
        var oldword := value
        if position = length (value) then
            value := oldword (1 .. * -1)
        elsif position = 1 then
            value := oldword (2 .. *)
        else
            value := oldword (1 .. position - 1) + oldword (position + 1 .. *)
        end if
    end remove

    %removes all whitespace from a word
    fcn as_stripped : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if oldword (i) ~= " " then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped

    procedure strip
        value := as_stripped
    end strip

    fcn size : int
        result length (value)
    end size

    fcn as_upper : string
        result Str.Upper (value)
    end as_upper

    procedure make_upper
        if original = "" then
            original := value
        end if
        value := Str.Upper (value)
    end make_upper

    fcn as_lower : string
        result Str.Lower (value)
    end as_lower

    procedure make_lower
        if original = "" then
            original := value
        end if
        value := Str.Lower (value)
    end make_lower

    fcn is_empty : boolean
        if value = "" then
            result true
        else
            result false
        end if
    end is_empty

    fcn originally : string
        result original
    end originally

    procedure make_original
        value := originally
    end make_original

    procedure gets
        get value : *
    end gets
end String

var my_string : ^String

new String, my_string

put "Please enter anything " ..
my_string -> gets

put "All lowercase"
put my_string -> as_lower

put "All uppercase"
put my_string -> as_upper

put "Stripped of whitespace"
put my_string -> as_stripped

put "String size"
put my_string -> size

put "Inserted \'a\' at position 1"
my_string -> insert ("a", 1)
put my_string -> content

put "Removing \'a\' at position 1"
my_string -> remove (1)
put my_string -> content

put "Original"
put my_string -> originally


its really quite readable (it almost reads like english Very Happy) so you can probably tell what most of these do. please post suggestions, improvements, constructive critisism etc.
Sponsor
Sponsor
Sponsor
sponsor
Hackmaster




PostPosted: Tue Dec 12, 2006 9:32 am   Post subject: (No subject)

Good Stuff, Freakman...but...

can't you do most of this stuff without this class? you can find the length of a word anyways, and I believe that you can to stuff like toupper and tolower work to0 (but that just might be me thinking in C). but the strip function is cool, and repalcing single letters and such.... that's getting pretty sweet. Hey, you are on your way to making FreeWord!

keep it up!
Clayton




PostPosted: Tue Dec 12, 2006 1:41 pm   Post subject: (No subject)

sure you can do those things without the class, but the whole point is to make the string an object. As an object, there is so much more freedom and flexibility.
ericfourfour




PostPosted: Tue Dec 12, 2006 3:27 pm   Post subject: (No subject)

If you want to make something very helpful, not that this isn't helpful, make a BigString class. It would remove the limit from the size of strings.
[Gandalf]




PostPosted: Tue Dec 12, 2006 7:56 pm   Post subject: (No subject)

Good idea, ericfourfour.

Freakman, not bad, though next time I suggest you do something like learning a new language in your bored time. Surprised

Also, the benefits of a String class are greatly diminished in Turing thanks to it's largely awkward OOP syntax.
Clayton




PostPosted: Tue Dec 12, 2006 9:45 pm   Post subject: (No subject)

Yes I know, but seeing as we're going to be forced of another year of Turing (doing OOP woohoo!), I figured I might as well try and do something to prepare for it....

I guess I could do something like make a module out of it (ie, like they do with most of the other modules, take a class and manipulate it with a module), but then things could get confusing...
wtd




PostPosted: Thu Dec 14, 2006 4:12 am   Post subject: (No subject)

You may wish to have separate get and get_line methods.

Also, may I suggest throwing in something a bit functional and having filtering methods which can taken a predicate function as an argument?
Clayton




PostPosted: Thu Dec 14, 2006 12:21 pm   Post subject: (No subject)

and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing?

EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Dec 14, 2006 12:52 pm   Post subject: (No subject)

Imagine you wanted to remove all punctuation from a string. So you write a method to do that.

Now you want a method that removes all whitespace. So you write a method to do that.

Except that those methods would be identical, save for one thing... the check to see if a character should be removed. So that can be factored out into separate functions, and those functions passed to a single, generic method.
Clayton




PostPosted: Thu Dec 14, 2006 12:54 pm   Post subject: (No subject)

not quite sure what you want me to do here. You're saying I should have a generic method that does all of the work, with other functions to be passed to it to specify exaclty what is needed?
rdrake




PostPosted: Thu Dec 14, 2006 12:56 pm   Post subject: (No subject)

Freakman wrote:
and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing?

EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying...
As wtd said below me, there can be slight differences between methods. This in Ruby is usually represented by adding either "!" or "?". For example "chomp" and "chomp!". There are only slight differences between them (one modifies the original value, one does not... but both return the modified value regardless), but they are still slightly different.
wtd




PostPosted: Thu Dec 14, 2006 12:56 pm   Post subject: (No subject)

Yes. All of the code that handles iterating through the string, and creating a new string... it's the same for both methods. The only thing that changes is how you check each character. So that gets factored out into separate functions.
Clayton




PostPosted: Thu Dec 14, 2006 12:59 pm   Post subject: (No subject)

What exactly do you mean by "How you check each character"? Does that mean I should have a function that tells the generic function what to remove from a string? If so, would this function be passed to the function from outside of the class, or internally?
wtd




PostPosted: Thu Dec 14, 2006 1:01 pm   Post subject: (No subject)

Well, to check for punctuation:

code:
function is_punctuation (ch : string) : boolean
   result (ch = "." or ch = "," or ch = "!" or ch = "?" or ch = ";")
end is_punctuation
Clayton




PostPosted: Thu Dec 14, 2006 1:05 pm   Post subject: (No subject)

okay, I see where you're going now, so just have said generic method take in that function to check for punctuation, if its punctuation remove it? Or am I still missing something?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: