Board index » delphi » toggle switch-convert uppercase to lowercase by a toggle switch

toggle switch-convert uppercase to lowercase by a toggle switch

Does anyone know how to write the code for converting from uppercase to
lowercase and vice-versa by exclamation mark?(period stops program)

example:

input:  Here is my question! about PASCAL.
output:  here is my question ABOUT pascal.

extremely stressed,
j9

 

Re:toggle switch-convert uppercase to lowercase by a toggle switch


Quote
Janene <JANENEBURNH...@prodigy.net> wrote:
> Does anyone know how to write the code for converting from uppercase to
> lowercase and vice-versa by exclamation mark?(period stops program)
> example:
> input:  Here is my question! about PASCAL.
> output:  here is my question ABOUT pascal.

Not too difficult:

Use a Boolean flag, e.g. ToggleCase.
Set it to "false" and parse the string one character at a time in the
following manner:
  If the character is '!', change ToggleCase.
  If the character is alphabetic, check ToggleCase to see if you need to
  change the case, and if so, change it; then write it to the output string.
  If the character is a period, write it and finish.
  For any other character, write it to the output string.

You can change the case of a character with UpCase and LowCase, except
you'll have to write LowCase yourself:

function LowCase (ch: char): char;
begin
  if ch in ['A'..'Z'] then
    LowCase := chr(ord(ch) + ord('a') - ord('A'))
  else
    LowCase := ch
end;

The main procedure could look something like this:

procedure ProcessString (instr: string; var outstr: string);
var
  ToggleCase, finished: Boolean;
  i: byte;

  function ToggleChar(ch: char): char;
  begin
    if ch in ['a'..'z'] then
      ToggleChar := UpCase(ch)
    else if ch in ['A'..'Z'] then
      ToggleChar := chr(ord(ch) + ord('a') - ord('A'))
    else
      ToggleChar := ch
  end;

begin
  i := 1;
  finished := false;
  ToggleCase := false;
  outstr := '';
  while (i <= length(instr)) and not finished do
    case instr[i] of
      '!': ToggleCase := not ToggleCase;
      '.':
        begin
          outstr := outstr + '.';
          finished := true
        end;
      else
        outstr := outstr + ToggleChar(instr[i])
    end  {case statement}
end;

Here I've combined the functionality of LowCase into ToggleChar, but you
don't have to do it this way.

This allows you to have multiple ! markers, but at the first . the process
will be terminated.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Re:toggle switch-convert uppercase to lowercase by a toggle switch


In article <8ik5rn$32...@enyo.uwa.edu.au>,
The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

Quote
>Janene <JANENEBURNH...@prodigy.net> wrote:

>> Does anyone know how to write the code for converting from uppercase to
>> lowercase and vice-versa by exclamation mark?(period stops program)

>> example:

>> input:  Here is my question! about PASCAL.
>> output:  here is my question ABOUT pascal.

>Not too difficult:

Come on, that was obviously homework.

Osmo

Other Threads