?
Solved

String check

Posted on 2003-03-18
20
Medium Priority
?
281 Views
Last Modified: 2010-04-16
I have to make a procedure that does the following (assuming that there is a string containing a record of a number constatnt):
1. Determines the type of the constant - integer, not defined etc
2. Warns for an error in the entered string
3. -OK- message if the string contains the right constants
4. Returns the string (the part with the right constant)

I was given some clues about using "set of char", "case <EXPRESSION> of" etc., but I'm new to Pascal and I can't really bring all this together. I will appriciate if you could give me some hints or provide me with the source at least  of a simular program. Thank you very much.
0
Comment
Question by:legolasbg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
  • +2
20 Comments
 
LVL 6

Expert Comment

by:MannSoft
ID: 8160937
If this is homework maybe you should try posting the exact question you were given as well as any examples (like what the string might look like).  From your summary I cant tell what it is that you need to do.

And if this isnt homework, I think you'll have to try rewording it or giving more information or something.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8161295
"a string containing a record" ? Are you talking Pascal here ? :D

agree with MannSoft, explain or give data samples (input, output expected)
0
 

Author Comment

by:legolasbg
ID: 8162163
I'm sorry for not being precise at all but I was pretty con=fused myself. First of all is it possible for a string to contain a constant. If possible let's assume that the user enters a string containing a constant. The procedure has to check the type of the constant and to warn via some messages in the entered string ("OK" if it contains correct constants; "Error"). Eventually it returns the part of the string with the correct constant. I translated the homework word by word.
That's the best I can do. Maybe theres a mistae somewhere or somthing :D . Even if it doesn't make any sense to you thanks annyway.
0
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
LVL 6

Expert Comment

by:MannSoft
ID: 8162471
Heh sorry but I still dont follow.  A string can contain the name of a constant.  For example:

const
  MyConst = 500;

var
  S: String;

begin
  S := 'MyConst';
end.

The string now contains the name of a constant.  But I dont think there is any way to check if it contains the name of a valid constant.  Not sure how it would be useful either.

Maybe you misread and the string is supposed to be made up of consonants?  That would be a much more common beginners question :)
0
 
LVL 15

Expert Comment

by:VGR
ID: 8162650
1) in which language is your homework assignment, untranslated ? :D
2) the string can also contain the string representation of the constant 500 itself : '500' but I don't understand how you would "test if it contains the right constants"

that's not clear at all
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8171175
Still haven't answered the question - Is this hoemwork?

mlmcc
0
 

Author Comment

by:legolasbg
ID: 8174595
Yes it's homework. And I  realy can't make it more clear because it ain't clear for me as well. I'm sorry for wasting     your time. The  whole problem is obviously not defined pretty well. I'd be glad if you could post me a link to some tutorials dealing with strings in Pascal. Thanks!

legolasbg
0
 
LVL 15

Expert Comment

by:VGR
ID: 8175745
strings are a very simple data structure in all languages not descending from ugly C.
It's akin to the vector (or 1D array).
thus it can be seen as a sequence of "boxes", each one containing a character (type = Char)

Thus is Pascal :
Program demo;
var st, st2 : String; // String is identical to String[80] at least originally
    i : Word; // or Integer, but Word is unsigned which is more logical for counting in N+
Begin
  st:='hello world!';
  // now concatenate an other string
  st:=st+' You are the ';
  // now transforman integer to a string before concatenation
  st2:=IntToStr(34);
  st:=st+st2+' visitor.';
  // display as string
  WriteLn(st); // equivalent to WriteLn(OutPut,st);
  // now display character per character as if it were a vector/array of chars
  for i:=1 to Length(st) Do WriteLn('character of rank = ',i,' is ',st[i]);
End.
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 8175799
Was it homework you were given verbally and you had to write down?  If not, you could try posting the question from the book word for word.  Then maybe we could get the correct context of the quesiton.

For instance, maybe by constant it doesnt mean

const
  SOMETHING = 0;

Maybe it means constant as in something like

G = Gravitational Constant
c = Speed of light

In which case you would do something like:

for I := 1 to Length(S) do
  case S[I] of
    'G': WriteLn('String Contains Gravitational Constant');
    'c': WriteLn('String Contains Speed Of Light');
    else
      WriteLn('String Contains Invalid Constant');
  end;
0
 

Author Comment

by:legolasbg
ID: 8177699
The homework was given verbally. I will meet the teacher on monday. So I will be able to post my question more precisely.
In the mean time I have a question which is not really an expert one :]... Is it possible to divide the points between the experts or I have to choose one specific answer 'cause I have obviously really wasted your time. I know that points mean nothing to you but yet I want to show my gratefulness. It won't be a tragedy even if I don't get the solution of my problem ... I'm just 11 old so I have a lot of time to learn. Thanks a lot. :D
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8178467
Thank you for your last comment.  We have a membership agreement which states we are not supposed to do homework.  That is the reason for the question.

Also you won't learn nearly as much if we do the problem for you.  Once you have the problem clearly stated we will be happy to help clarify, answer specific questions as to why the code you wrote doesn't work, how a specific function works, etc.  Just don't expect us to do the whole thing for you.

It is possible to divide the points between experts.  You need to submit a request in the Community Support forum asking them to split the points.  At this time unless you feel your question has been answered, I see no reason to do that.  It won't hurt to wait until Monday and add comments clarifying the assignment along with your first stab at the problem.

mlmcc
0
 
LVL 15

Expert Comment

by:VGR
ID: 8179344
for homework, I suppose it's allowed to answer by giving guidelines ?
0
 

Author Comment

by:legolasbg
ID: 8180431
My teacher posted the source on his site. Obviusly i missleaded you about some parts of the problem. This is the solution. Ofcourse he espects us to write our own source. This was our first lesson so I guess he wanted to help us. I didn't know about the agreement concerning homeworks. Sorry about that. Here is the source(I use Borland Delphi so don't  mind some parts of the source in the beginning):

program  setstr;

{$APPTYPE CONSOLE}

uses  SysUtils;

type ChSet=set of Char;

     token=(int,rea,none);

const digits=['0'..'9'];

      toknames:array[token] of string[10]=('integer','real','none');

function GetCh(var s:string):char;

begin

 GetCh:=s[1];s:=copy(s,2,255);

end;
procedure ClrCh(var s:string);

var L:integer;

begin

 L:=length(s);

 while(L>0)and not(s[L] in digits)do

 begin setlength(s,L-1);Dec(L); end;

end;
function NumConst(InChar:string; var t:token; var NumOK:boolean):string;

var stat:BYTE;

       c:char;

       s:string;

       v:ChSet;

begin

  s:='';

  t:=none;

  stat:=0;

  v:=digits+['+','-'];

  InChar:=InChar+#13;

  c:=GetCh(InChar);

  while (stat<8)and (c in v) do

   begin

    s:=s+c;
    case stat of

    /
        0:if (c='+') or (c='-')then

          begin stat:=1;v:=digits;end else

          begin v:=digits+['.','e','E',#13];stat:=2;t:=int;end;

            1:begin v:=v+['.',#13,'e','E'];stat:=2;t:=int;end;

         2:case c of

           #13:begin stat:=8;end;

           '.':begin stat:=3;v:=Digits;t:=rea;end;

           'e','E':

             begin stat:=5;v:= Digits+['+','-'];t:=rea;end;

          end;{case 2}

          3:begin stat:=4;v:=v+['e','E',#13];end;

            4:case c of

          #13:stat:=8;
          'e','E':begin stat:=5;v:=Digits+['+','-'];end;
          end;{case 4}

           5:if c in Digits then

          begin stat:=7;v:=Digits+[#13];end else

          begin stat:=6;v:=Digits;end;

          6:begin stat:=7; v:=v+[#13];end;

          7:if c=#13 then begin stat:=8;end;

      end;{case}

        if stat<8 then c:=GetCh(InChar);

  end;

  NumOK:=stat=8;
  ClrCh(s);
  NumConst:=s;

end;

var q,s:string;t:token;b:boolean;

begin

repeat

  Write(' Input string:');readln(s);

  q:=NumConst(s,t,b);

  if b then

   begin

    writeln('Correct');

    writeln('The token is: ',q,' of type ',toknames[t]);

   end else

   begin

    writeln('Wrong symbol found. The source string was: ',s);


    q:=NumConst(q,t,b);

    writeln('The recognized part is: ',q,' of type ',toknames[t]);

   end;

until false;

end.

I'll be glad if you have any suggestions about changing my teacher's method 'cause it wont's be a real homework if I copy & paste his code. I guess that this doesn't violate your agreement. I'm not asking for the whole source. Just for some hints. Thanks.

0
 
LVL 15

Accepted Solution

by:
VGR earned 200 total points
ID: 8180539
first of all you teacher is doing things the way I like it, but slightly violating good practices :
manipulating InChar:string via setlength, passing it as a Var parameter, and adding chars to it, while it's a const parameter in the scope of the function ;-)
(const = not var)

what I would recommend is to forget about the getCh() that "decrements" the string, and the process character by character

Try to build your own parsing/tokenizing using Pos(), Copy(). Do it in two passes if possible : first separate possible constants (if they are separated), then analyse each one :
-if 'E' or 'e' present, chances are it's a real in scientific notation (beware : both teacher's and your code will then be unable to cope with hexadecimal constants ;-)
-if '.' then it's a real in mantissa+decimals writing
-if val() returns a zero value, it's a string
-else it's an integer of some sort
-check for leaduing + or - to get sign and thus type (Word/Integer) you may even count the number of digits (Length()) to see whether it's a LongInt/LongWord and beat the teacher's code

good luck
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8189430
If the code posted is the answer then you have no need to assign points.  You can go to Community Support

http://www.experts-exchange.com/Community_Support/

and post a zero point question asking them to PAQ this question and refund your points as you solved this problem yourself.  You can use the points for other questions.

Quote this question number so they know where it is.

http://www.experts-exchange.com/Programming/Programming_Languages/Pascal/Q_20554834.html

Homework is a NO NO but in this case it's OK as we are guiding you rather than doing the assignment for you.

There is also a Delphi forum here as well.

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/

The code posted is really advanced stuff for a student.  This is a beginning course?
0
 

Author Comment

by:legolasbg
ID: 8194515
Thanks for the detailed informtion. I believe that what VGR posted is the answer because as I said  the source I posted is the teacher's code :) . I'm still working on it as I had no time during the weekend ;). I think I should asign my points to him/her (I don't know what VGR means). And about your qustion <dbrunton> the answer is "yes"
it's a beginning course. I studied Pascal last term as well but as I'm new to relatively programming I find most of my homeworks as "advanced stuff". In my Bulgaria Informatics is a  "must" course in all schools. I study in an English language school. I'd like to know for if I should asign the points after I reach the actual solvation of my problem? Thanks a lot.  
0
 
LVL 15

Expert Comment

by:VGR
ID: 8194573
depends.
If you found some comment here valuable in reaching your solution, then give him/her/them (post a new pseudo-quesiton with "points for Mr X") the answer and a grade "A" or "B" depending if the answer was "thorough" or not.
Note : given that this is homework and we are theoretically forbidden to give the complete "thorough" answer, grade "A" seems most appropriate)

If you found nothing in here but general clues, give a "C"
If you found nothing useful here, ask in CS for a refund 8-)

best regards,

and FYI, VGR is my trigram and V is for Vincent
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 8200298
VGR deserves the points.

And to be a good programmer, one must program, program.  Reading other people's code is also good.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8201188
essential, I would say. I'm the (not rare) type who learnt "sur le tas" by expanding others' code (books, etc) at the beginning.
0
 

Author Comment

by:legolasbg
ID: 8202037
Thank you all!!! :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question