Hello! I'm just new here, and I have just completed a short pascal tutorial. So, in this n00by thread I wanna ask some simple stuff, and sorry about the incovience (you will face many questions lol) but understand that this is my only way to learn something I really love :)

First of all can you explain me the inc and length commands? :?:

Second question :icon_cheesygrin: I'm trying to code a program that counts how many characters are in a text, and then find how many A's how many B's etc (and spaces). Only (a...z and character space counts). I'm sure I must use an array, but I'm a bit confused, so can you give me the main idea please? (I'll also use eof :P)

Third, is there any possibility that a program can read ASCII coded stuff? and write in ASCII ?
And if yes, how? :)

Last question (for now) :icon_evil: How to make my program outputs some stuff per line? For example how can i make the program from the second question outputs how many A's and B's in this way:

A 17
B 18

I want to make that output in format :
character<space>count

That's all the question. Hope you can help. Thanks in advance and for your time

-Tragedian Of Sullen Skies

Recommended Answers

All 16 Replies

Where from did you cut and paste the homework ?

One friend gave me the exercise :P ... but i can't solve it. and the bad thing is that i can't find him on msn or.... but i really wanna solve that, its a great learning expirience

but understand that this is my only way to learn something I really love

Actually, this is not your only way . . . which leads directly into my question of, "Have you attempted to code the Pascal/Delphi to accomplish this task?

Of course I have... But I can't really make it work :P I'm not the type '' do the work for me '' . I have tried and i can't really make it up and working. Also I don't ask for coding the program for me. I just want help with the basic idea. Also why you try attacking me? I didn't require something, I just ask for help, which you can give ONLY if you want to

sullecy,

Please, try not to be quite so defensive and don't immediately assume the role of victim. I was not attacking you but asking you a question to establish a basis for helping you.

I am going to assume that you are working in straight Pascal and not in Delphi (this is a Pascal/Delphi list, so don't get all defensive on me again ;) ).

First of all can you explain me the inc and length commands? :?:

inc(x_integer) is a procedure that increments the integer valued variable (x_integer in this case) that is passed as the argument of the procedure.

Length(a-string) is a function that returns the number of characters in the string, in this case, or the number of items in an array, if I remember correctly. (It has been a little while since I coded in pure Pascal ;) ).

Second question :icon_cheesygrin: I'm trying to code a program that counts how many characters are in a text, and then find how many A's how many B's etc (and spaces). Only (a...z and character space counts). I'm sure I must use an array, but I'm a bit confused, so can you give me the main idea please? (I'll also use eof :P)

You can get the length of the string and then perform a FOR loop to "walk" through the string checking each character and incrementing a separate counter for each characters you want to count by using an IF statement.

Third, is there any possibility that a program can read ASCII coded stuff? and write in ASCII ?
And if yes, how? :)

How you would do that kind of depends on what machine you are working on . . . but, if it is a PC, then that is what it is already doing.

Last question (for now) :icon_evil: How to make my program outputs some stuff per line? For example how can i make the program from the second question outputs how many A's and B's in this way:

A 17
B 18

I want to make that output in format :
character<space>count

That's all the question. Hope you can help. Thanks in advance and for your time

-Tragedian Of Sullen Skies

WriteLn will output a line of text and then perform a CRLF (Carriage Return Line Feed) which moves it to the beginning of the next line. Look up WriteLn for the exact syntax that you will need based upon your Pascal compiler.

Thanks for the answer! Yes, i'm coding in pure pascal! Sorry for ' the defensive stance' but you know, i got your answer like ' try alone noob i won't help you or so' (my fault, reading it again made me understand that you don't mean that :) I will try on what you say, and if i need to ask some more things i will!!! Haha

Well i figured out this:

var
  Counts: array[00..26] of integer;

procedure CountCharacters(Data: string);
var
  i: integer;
  ch: char;
begin
  for i := 1 to length(Data) do begin
    ch := Data[i]; { get a character from the string }
    if ch = ' ' then
      inc(counts[26]); { increase space count }
    if (ch >= 'a') and (ch <= 'z') then
      inc(counts[byte(ch)-byte('a')]); { increase letter count }
  end;
end;

A...Z is 0...25 and space is 26. (with ummm ...little help i made it :P)

but the real question now is!!! how to output that? lol. I mean i understood how it works and its cool etc but how can i really output the letter count?

if i use this -> write (fileout, counts[26]); for example, i keep on getting as result 0, but i have much spaces in the string. same goes on every count. what to do? (give me a clue, so i can really understand how arrays work lol

Well i figured out this:

var
  Counts: array[00..26] of integer;

procedure CountCharacters(Data: string);
var
  i: integer;
  ch: char;
begin
  for i := 1 to length(Data) do begin
    ch := Data[i]; { get a character from the string }
    if ch = ' ' then
      inc(counts[26]); { increase space count }
    if (ch >= 'a') and (ch <= 'z') then
      inc(counts[byte(ch)-byte('a')]); { increase letter count }
  end;
end;

A...Z is 0...25 and space is 26. (with ummm ...little help i made it :P)

but the real question now is!!! how to output that? lol. I mean i understood how it works and its cool etc but how can i really output the letter count?

if i use this -> write (fileout, counts[26]); for example, i keep on getting as result 0, but i have much spaces in the string. same goes on every count. what to do? (give me a clue, so i can really understand how arrays work lol

Okay, first you might want to make sure that your compiler is case insensitive because you define your array as Counts and then reference it as counts.

Next, I would probably set up another integer that I would use to create the index value into the array:

var
  Counts: array[00..26] of integer;

procedure CountCharacters(Data: string);
var
  i: integer;
  ndx: integer;   {The integer index I suggested}
  ch: char;
begin
  for i := 1 to length(Data) do begin
    ch := Data[i]; { get a character from the string }
    ndx = byte(ch)-byte('a');  [Calculate the Array Index}
    {for degub purposes . . . chr(13) is CR and chr(10) is LF}
    {you may need to slightly adjust the syntax of the following line}
    write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13)  chr(10));  
    if ch = ' ' then
      inc(counts[26]); { increase space count }
    if (ch >= 'a') and (ch <= 'z') then
      inc(counts[ndx]); { increase letter count }
  end;
  
  for i := 0 to 26
     write(i + ':' + counts(i) + chr(13)  chr(10));  
  end;  {for}
end;

No Pascal compiler is case-sensitive. Pascal is explicitly case-insensitive.

No Pascal compiler is case-sensitive. Pascal is explicitly case-insensitive.

I always avoid "always" and I try never to use "never."

I would not categorically deny the existence of a case sensitive Pascal, just as I would not make a practice of declaring a variable using one spelling (e.g. "Camel-case") and then reference it using another (e.g. lower- or uppwer-case).

Personally, my preference agrees with yours.

However, when the pascal specification says that identifiers are not case-sensitive I feel that the word always is correct and justifiable. If you ever find a pascal compiler that complains about case-sensitivity then it is not valid (i.e. standard) pascal.

Yea, yea I know it becomes tiring, but this time its some error in your code!!

var

Counts: array[00..26] of integer;
filein, fileout : text;

procedure CountCharacters(Data: string);
var
  i: integer;
  ndx: integer;   {The integer index I suggested}
  ch: char;
  fileout : text;
begin
  assign (fileout, 'D:\programming.fileout.txt');
  rewrite (fileout);
  for i := 1 to length(Data) do begin
    ch := Data[i]; { get a character from the string }
    ndx = byte(ch)-byte('a');  {Calculate the Array Index}
    {for degub purposes . . . chr(13) is CR and chr(10) is LF}
    {you may need to slightly adjust the syntax of the following line}
    write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13)  chr(10));
    if ch = ' ' then
      inc(counts[26]); { increase space count }
    if (ch >= 'a') and (ch <= 'z') then
      inc(counts[ndx]); { increase letter count }
  end;

  for i := 0 to 26
     write(i + ':' + counts(i) + chr(13)  chr(10));
  end;  {for}
end;

begin
assign (filein, 'D:\programming\filein.txt');
reset (filein);
assign (fileout, 'D:\programming\fileout.txt');
rewrite (fileout);
read (filein, malakies);
Countcharacters(malakies, fileout);
close (filein);
close (fileout);
end.

So... lets see what errors we get...

1st. <17,29> illegal expression:
it talks about this line:

ndx = byte(ch)-byte('a');

dunno what to do with it... i bet its something with the quotes...

2nd. <20,57> Incompatible types got ''small int'' expected ''short string''
its on this line:

write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13)  chr(10));

focusing on:

Array Index: [' + ndx + ']' + chr(13)  chr(10));

This line is very confusing...

3rd. <20, 78> Syntax error ')' expected but identifier ''CHR'' found. darn i tried adding an ')' but when i do, it gives more errors.
its on this line :

write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13)  chr(10));

That's all!! yea i know couldn't be more!!
Also, if you could give a brief description on what and why your doing (very short) It would be great.

SO, i'm sorry on more time for being tiring, but are my gods lol...

1st. <17,29> illegal expression:
it talks about this line:
ndx = byte(ch)-byte('a');
dunno what to do with it... i bet its something with the quotes...

2nd. <20,57> Incompatible types got ''small int'' expected ''short string''
its on this line:

write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13) chr(10));

focusing on Array Index: [' + ndx + ']' + chr(13) chr(10));

This line is very confusing...

3rd. <20, 78> Syntax error ')' expected but identifier ''CHR'' found. darn i tried adding an ')' but when i do, it gives more errors.
its on this line :
write(fileout, 'ch: [' + ch + ']; Array Index: [' + ndx + ']' + chr(13) chr(10));
[/code]
That's all!! yea i know couldn't be more!!
Also, if you could give a brief description on what and why your doing (very short) It would be great.

SO, i'm sorry on more time for being tiring, but are my gods lol...

FIrst error is due to my having omitted the ":" ("=" instead of ":=").

The other errors, to be honest, I was kind of expecting. Those lines are intended to concatenate the quoted (text) material with the value from the integer(s). In some cases, one can get away with that basic format and in others one has to set up a string variable that one builsds up with the pieces one wants to output. Usually, one also has to use a conversion routine (e.g. chr(ndx)) to convert an integer value to a string that one can then concatenate with another string. Those output lines are intended to provide you with someting along the lines of:

ch: [a]; Array Index: [0] 
ch: [e]; Array Index: [5] 


0: 22
1: 12
2: 0
3:19
etc.

(If you have a Pascal language reference, you should be able to look up how to format the output.)

The intention is to provide some debugging output along the way. THe first "write" is intended to provide an output line for every character as it is processed so that you can see the character and the array index value that is going to be used. The second one is intended to list the values by array index.

I am about to leave work and head home. I don't know what time zone you are in but I am in the -6 time zone (either that or the -5 . . . Central Standard Time in the US, anyway :P ).

You are missing plus signs between chr(13) and chr(10).

Since we are using a BP dialect, you could also just say + #13#10 For the ndx, use the ord() function ndx := ord(ch) -ord('a'); Hope this helps.

Duoas,

Good catch on the "ord" vs "byte" and I really thouight I had hit the "+" key . . . shows what thinking does for you. ;)

Heh, I often find that things get lost between my brain and the keyboard too... :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.