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

Username:   Password: 
 RegisterRegister   
 Recognizing spaces as "Space"
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Da_Big_Ticket




PostPosted: Mon Oct 17, 2005 10:36 am   Post subject: Recognizing spaces as "Space"

So far I have my program in working order. It is able to find the middle letter(s) of any word or phrase entered. However, one component of the program marking scheme is that the program is supposed to recognize spaces, and instead of displaying them literally as a space, the program needs to say "Space" I will post my working code as I have it, if you can fix it i would be very appreciative, its under the calculate procedure. Thanks

code:

import GUI in "%oot/lib/GUI"

% --------------------------------------------------- Variable Table -------------------------------------------------------
var key, input_char : string (1)
var word : string := ""
var winID, oddeven, middle1, middle2, lengthword, introscreen, layoutscreen, closingscreen, font1, font2 : int
% ------------------------------------------------------ End Table ---------------------------------------------------------

% ---------------------------------------------- Values for Pictures, Fonts ------------------------------------------------
introscreen := Pic.FileNew ("e:/turinglayout4intro.jpg")
layoutscreen := Pic.FileNew ("e:/turinglayout4.jpg")
closingscreen := Pic.FileNew ("e:/turinglayout4close.jpg")
font1 := Font.New ("arial:10")
font2 := Font.New ("comicsansms:20")
% ------------------------------------------------------ End Values --------------------------------------------------------

% ----------------------------------------------------- Open Window --------------------------------------------------------
winID := Window.Open ("position:middle,center, graphics: 640;480, Title:Middle Letter Machine")
% This opens a window with a specific position on screen, a speicifc size, and with a custom title
% ------------------------------------------------------ End Window --------------------------------------------------------
% -------------------------------------------------------- Music -----------------------------------------------------------
process coldplay
    loop
        Music.PlayFile ("e:/coldplay.MP3")
    end loop
end coldplay
% This opens a music file, is called later in the program, and repeats until the program is exited
% ------------------------------------------------------ End Music ---------------------------------------------------------

% ------------------------------------------------ Finding Middle Letters --------------------------------------------------
procedure calculate
    if lengthword div 2 = lengthword / 2 then
        Font.Draw ("Your word contains an even number of letters", 290, 70, font1, white)
        middle1 := lengthword div 2
        middle2 := middle1 + 1
        Font.Draw ("Your middle letters of the word inputted are", 290, 50, font1, white)
        Font.Draw (word (middle1), 370, 30, font2, white)
        Font.Draw ("And", 395, 30, font1, white)
        Font.Draw (word (middle2), 430, 30, font2, white)
    else
        Font.Draw ("Your word contains an odd amount of letters", 290, 70, font1, white)
        middle1 := lengthword div 2 + 1
        Font.Draw ("Your middle letter is", 350, 50, font1, white)
        Font.Draw (word (middle1), 410, 30, font2, white)
    end if
end calculate
% -------------------------------------------------- End Middle Letters ----------------------------------------------------

% ---------------------------------------------------- Closes Window -------------------------------------------------------
procedure exitprog
    Window.Close (winID)
end exitprog
% This procedure is used to completely close the run screen, is called later in program by the button "Quit"
% -------------------------------------------------- End Close Window ------------------------------------------------------
% ---------------------------------------------------- Closing Screen ------------------------------------------------------
procedure finish
    cls
    Pic.Draw (closingscreen, 0, 0, picCopy)
    var exitprogram : int := GUI.CreateButton (146, 37, 350, "Click Here To Completly Close The Program", exitprog)
end finish
% -------------------------------------------------- End Closing Screen ----------------------------------------------------
% ---------------------------------------------------- Main Program --------------------------------------------------------
procedure complete
    View.Set ("nocursor")
    Pic.Draw (introscreen, 0, 0, picCopy)
    fork coldplay
    getch (key)
    cls
    Pic.Draw (layoutscreen, 0, 0, picCopy)
    locate (maxrow div 1.3, round (maxcol / 1.8))
    get word:*
    lengthword := length (word)
    var calculator : int := GUI.CreateButton (17, 252, 120, "Calculate", calculate)
    var repeatButton : int := GUI.CreateButton (17, 148, 120, "Repeat", complete)
    var quitButton := GUI.CreateButton (17, 30, 120, "Quit", finish)
    loop
        exit when GUI.ProcessEvent
    end loop
end complete
% --------------------------------------------------End Main Program -------------------------------------------------------


complete

Sponsor
Sponsor
Sponsor
sponsor
bass_maniac




PostPosted: Mon Oct 17, 2005 11:17 am   Post subject: (No subject)

First you can change
code:
if lengthword mod 2 = lengthword / 2 then

to
code:
if lengthword mod 2 = 0

just because it's more efficient.

Here is the modified code.
code:
procedure calculate
    if lengthword mod 2 = 0 then
        Font.Draw ("Your word contains an even number of letters", 290, 70, font1, black)
        middle1 := lengthword div 2
        middle2 := middle1 + 1
        Font.Draw ("Your middle letters of the word inputted are", 290, 50, font1, black)
        if word (middle1) = " " then
            Font.Draw ("Space", 320, 30, font2, black)
        else
            Font.Draw (word (middle1), 370, 30, font2, black)
        end if
        Font.Draw ("And", 395, 30, font1, black)
        if word (middle2) = " " then
            Font.Draw ("Space", 430, 30, font2, black)
        else
            Font.Draw (word (middle2), 430, 30, font2, black)
        end if
    else
        Font.Draw ("Your word contains an odd amount of letters", 290, 70, font1, black)
        middle1 := lengthword div 2 + 1
        Font.Draw ("Your middle letter is", 350, 50, font1, black)
        Font.Draw (word (middle1), 410, 30, font2, black)
    end if
    View.Update
end calculate


Basically I just searched the word at middle1 and middle2 to see if that character was a space and output a different string if it was. You can learn about searching strings in http://www.compsci.ca/v2/viewtopic.php?t=8229.

Note: I changed the text to black, because none of us will have your background pics. You can change it back to white, though.
jamonathin




PostPosted: Mon Oct 17, 2005 11:17 am   Post subject: (No subject)

Im not shure if i understand what you mean.

You're now looking for spaces in the word/phrase entered. Do you want to tell the user how many spaces they entered? are you goign to squeeze the word SPACE in where the actual 'space' is? Do you want to wipe out the spaces and calculate that way?

To find a space all you need to check for is.

code:

var word:string:="BLAH BLAH B00GER"
for i: 1 .. length(word)
if word (i)=" " then
put "fart (SPACE)"
end if
end for
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: