Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USE OF 'UPCASE' IN PASCAL

Status
Not open for further replies.

PROFESSORSPARKIE

Instructor
Oct 24, 2002
181
US
I have tried every combination of use for the 'upcase' function and keep getting mismatch data types and i am only using char & strings.

I want to read a file and loop through the input string record changing all characters to upper case and then print the records.

I have written many programs in many languages but this has me stumped.

I tried it in turbo and q-pascal. I prefer q-pascal for standalone filter programs.

any help? I just need the record loop statements 6 or 7.

thanks in advance.
 

I Actually use FP but if you create a small program like;

Code:
VAR
InChar : CHAR;

BEGIN
    WRITE(Enter a character: ');
    READLN(InChar);
    WRITELN(Upcase(InChar));
END.

Does this work?

what about...

Code:
VAR
A : RECORD
        n : INTEGER;
        m : CHAR;
    END;

BEGIN
    READLN(A.m); 
    WRITELN(UPCASE(A.m));
END.
 
Your answer looks simple enough but I am trying to read a line record into a string and make sure each character in the string is upper case and then print out the record string. My problem is the non support of upcase on a string and getting each character out of the string convert and build the string back.

This simple filter logic works for many utilities like striping null records, adding a header to a file, print only certain records like the FIND command.

Here is my program code. any more suggestions?

PROGRAM UP_CASE;
Uses CRT, printer;
VAR
DATAFILE : TEXT;
INREC : STRING;
OUTREC : STRING;
I : WORD;
C : CHAR;
S : STRING;
BEGIN
Assign( DATAFILE, '' );
Reset(DATAFILE);
ASSIGN(LST, '');
REWRITE(LST);
WHILE NOT Eof( DATAFILE ) DO
BEGIN
OUTREC := '';
READln(DATAFILE, INREC);
FOR I := 1 TO LENGTH(INREC) DO
BEGIN
S := COPY(INREC,I,1);
C := S;
C := UPCASE(C);
OUTREC := OUTREC + C;

END;
Writeln(LST, OUTREC );
END;
CLOSE(DATAFILE);
CLOSE(LST);
END.
 
upcase() is for a character only. It's not uncommon for me to have to copy a function I made into the text to handle this job for an entire string:

something like (untested since I just typed it out this time):

Code:
function upstr(instr: string): string;
  var
    i: byte;
    outstr: string;
  begin
    outstr := instr;
    for i := 1 to length(outstr) do
      outstr[i] := upcase(outstr[i]);
    upstr := outstr;
  end;

Not really too much of a trouble to do, but must be done.
 
TO GLENN9999

THANKS - it seems like a char variable could be assigned a byte directly from a string with the copy function using length 1 but it gets rejected. Looks like a suggestion for a compiler upgrade if these compilers were being maintained.

I first tried below, but no luck.

c := COPY(INREC,I,1);

then I could have used concat to put it back together.
again THANKS, I will try it.
 
TO GLENN9999

I can't believe how stupid I am. With over 44 years of programming experience on mainframes to PCs and I looked all around the solution. Your function reminded me that I could do direct indexing on each byte in a string like a character variable. I was trying to use the "copy" function just as I would a "substr" function in "perl" or "mid$" function in "BASIC or "line.substring" in "java", etc.

PASCAL just doesn't work exactly the same. Close but not.

I just changed

c := COPY(INREC,I,1);
to
c := INREC;

and it worked like a charm.

THANKS for the tip.

Professorsparkie
 
to glenn9999

I must have spent 5-6 hours off & on trying to force PASCAL to work the way I wanted.

I can't wait to tell my students. I have told them that programming is the most rewarding-frustrating activity in the computer field with mountain tops and valleys.

Again THANKS
professorsparkie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top