How to move all special characters to the beginning of a string in Pascal

1 Answer

0 votes
program MoveSpecialChars;

function IsAlphaNum(ch: char): boolean;
begin
  IsAlphaNum := ((ch >= 'A') and (ch <= 'Z')) or
                ((ch >= 'a') and (ch <= 'z')) or
                ((ch >= '0') and (ch <= '9'));
end;

function MoveSpecialCharactersToBeginning(s: string): string;
var
  i: integer;
  specials, chars: string;
  ch: char;
begin
  specials := '';
  chars := '';
  for i := 1 to length(s) do
  begin
    ch := s[i];
    if IsAlphaNum(ch) or (ch = ' ') then
      chars := chars + ch
    else
      specials := specials + ch;
  end;
  MoveSpecialCharactersToBeginning := specials + chars;
end;

var
  s, resultStr: string;
begin
  s := 'c++14$c&^java*(rust) php <>/python 3.14.2';
  
  resultStr := MoveSpecialCharactersToBeginning(s);
  writeln(resultStr);
end.



(*
run:

++$&^*()<>/..c14cjavarust php python 3142

*)

 



answered Dec 12, 2025 by avibootz

Related questions

...