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

Username:   Password: 
 RegisterRegister   
 Removing multiple spaces and performing a wordcount on a text file
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
yazdmich




PostPosted: Fri Oct 26, 2012 6:06 pm   Post subject: Removing multiple spaces and performing a wordcount on a text file

What is it you are trying to achieve?
Remove multiple spaces from a text file


What is the problem you are having?
Spaces are not removed


Describe what you have tried to solve this problem
Loops, different if statements


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
see attached file for the text to be parsed

Turing:


var line1, line2, before, after : string
var StreamNum1, StreamNum2, junkfound1, junkfound2, space2, space3, space4 : int

open : StreamNum1, "junkfood.txt", get

if StreamNum1 < 0 then
    put "File Access error: 404 or file not readable"
else
    open : StreamNum2, "junkfoodfixed.txt", put
    loop
        exit when eof (StreamNum1)
        get : StreamNum1, line1 : *
        junkfound1 := index (line1, "Junk Food")
        junkfound2 := index (line1, "junk food")
        if junkfound1 > 0 then
            before := line1 (1 .. junkfound1 - 1)
            line2 := "Unhealthy Treats"
            after := line1 (junkfound1 + 9 .. *)
            line1 := before + line2 + after
        elsif junkfound2 > 0 then
            before := line1 (1 .. junkfound2 - 1)
            line2 := "unhealthy treats"
            after := line1 (junkfound2 + 9 .. *)
            line1 := before + line2 + after
        end if
        put : StreamNum2, line1
    end loop
    close : StreamNum1
    close : StreamNum2
end if

open : StreamNum1, "junkfoodfixed.txt", get

if StreamNum1 < 0 then
    put "File Access error: 404 or file not readable"
else
    open : StreamNum2, "junkfoodfixed2.txt", put
    loop
        exit when eof (StreamNum1)
        get : StreamNum1, line1 : *
        space4 := index (line1, "    ")
        space3 := index (line1, "   ")
        space2 := index (line1, "  ")
        if space4 > 0 then
            before := line1 (1 .. space4)
            after := line1 (space4 + 3 .. *)
            line1 := before + after
        end if
        if space3 > 0 then
            before := line1 (1 .. space3)
            after := line1 (space3 + 2 .. *)
            line1 := before + after
        end if
        if space2 > 0 then
            before := line1 (1 .. space2)
            after := line1 (space2 + 1 .. *)
            line1 := before + after
        end if
        put line1
        put : StreamNum2, line1
    end loop
    close : StreamNum1
    close : StreamNum2
end if




Please specify what version of Turing you are using
OpenTuring 1.1



junkfood.txt
 Description:
Text to be parsed

Download
 Filename:  junkfood.txt
 Filesize:  2.47 KB
 Downloaded:  116 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Fri Oct 26, 2012 10:32 pm   Post subject: Re: Removing multiple spaces and performing a wordcount on a text file

Consider this:
Turing:
const myString : string := "abcdefghijklmnopqrstuvwxyz"
put myString(1 .. 12)
put myString(12 + 1 .. 26)


% output
abcdefghijkl
mnopqrstuvwxyz
evildaddy911




PostPosted: Sat Oct 27, 2012 1:02 pm   Post subject: RE:Removing multiple spaces and performing a wordcount on a text file

for the multiple spaces: don't restrict yourself to a max of 3 spaces in a row.
for the wordcount, determine what characters separate each word.
eg. should "joe-bob" count as 1 or or 2? should "joe.bob" count as 1 word or 2? How many for "Joe. Bob"?
simply count up the number of those characters and there you go
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: