views:

267

answers:

8

I have to write a program in Pascal which has to detect how many words on a text (input by the user) start with a certain letter. I can't use arrays, can you give me any hints as to where to start?

+3  A: 

If you know which letter, you merely need to keep a counter, no need for arrays.

If you don't know which letter, keep 26 counters. Stupid, but works as per your spec.

DVK
If you don't know which letter and can't use arrays, then the homework setter needs to be shot. You'd have to use a case statement (26-ways, or maybe 52-ways, unless you live in Europe and need to deal with accented letters) and increment ... oh heck; it doesn't bear thinking about. It is hard enough to be using Pascal without having to use it with one hand tied behind your back.
Jonathan Leffler
Unless the homework setter is trying to weed out the meek and the weak. Some programming tasks, you will eventually need to have the grit to do the equivalent of 26 counters :(
DVK
+1  A: 

First thing to do is define the set of characters that constitute letters, or conversely which ones constitute non-letters.

Write a function that takes a character and returns a boolean based on whether that character is a letter. Then loop through the string and call it for each character. When you detect a letter right after a non-letter or at the start of the string, increment your counter if it is the target letter.

JohnFx
A: 

count instances of SPACE LETTER plus first word if it matches.

aspitzer
A: 

(S) is your input string;

  1. Create a for loop that goes from 1 to the length of (S) - 1.
  2. Inside loop, check is (S)[i] = ' ' and (S)[i+1] = 't' where i is the loop counter and 't' is the letter starting the word you want to count
  3. If criteria in step two matches then increment a counter.

Note the minus one on the loop size.

Also, remember that the very first letter of the string may be the one you want to match and that will not get picked up by the loop defined above.

If you need to make your code smarter in that it can locate a specific letter rather than a hardcoded 't' then you can pass the requested character as a parameter to the function/procedure that your loop is in.

Steve Claridge
A: 

Off the top of my head - not tested

function WordCount(const S: string; const C: Char): Integer;
const
  ValidChars: Set of Char [A..Z, a..z]; // Alter for appropriate language
var
  i : Integer;
  t : string;
begin
  Result := 0;
  if Length(S) <> 0 then
  begin
    t := Trim(S); // lose and leading and trailing spaces
    t := t + ' '; // make sure a space is the last char
    repeat
      if (t[1] in ValidChars) and (t[1] = C then
        inc(Result);
      i := Pos(' ', t);
      t := Copy(t(i+1, Length(t));
    until Length(t) = 0;
  end;
end;

Why would you need an array or a case statement?

Despatcher
-1 He asked for hints, not the complete solution.
Ruben Steins
I thought it was right to show an example because the questioner has loaded the question towards "Array-thinking" by stating that he can't use them. When it was never an array-based problem.
Despatcher
A: 

Thks all, the problem was solve =D.

diego
Diego - the proper etiquette on StackOverflow is to (1) Mark whichever answer provided the best solution to you as "accepted", so that people don't bother answering the question anymore; and (2) state that your problem was solved as a comment (instead of an asnwer, unless you post an actual solution you developed which is different from anything posted so far). Hope this clarifies, all the best!
DVK
A: 

Va rog frumos...pentru a avea o nota buna am nevoie de ajutori vostru ..... trebuie sa rezolv 3 probleme in Pascal... am ceva idei dar nu sunt sigur kare ma poate ajuta mersi mult... Va Rogg.... Problemele sunt:

1 Trei prieteni X,Y,Z au procurat impreuna o mingeal carei cost constituie 250 lei.Sa se scrie un program care calculeaza suma achitata de fiecare , daca se stie ca Y a achitat 35% din suma totala , iar Z a achitata cu 15 % mai mult decit Y.

2 Se considera doua numere intregi.Sa se scrie un program care atribuie variabilei Logice T valoare True,daca numarul de date sunt egale , in caz contrar Valoare False.

3 Se considera doua numere intregi . Sa se afiseze primul numar , daca el este mai mare decit al doilea , in caz contrar sa se afiseze ambele numere ... Mersi mult.!! Astept Rs. Plizzz Help ME!

cornel
A: 

Please nice. . . to have a good score I need your help. . . . . must solve three problems in Pascal. . . I have some ideas but not sure I can help thanks a lot kare. . . Rogge will. . . . Problems are: 1 Three friends X, Y, Z have bought together a mingeal whose cost is 250 lei. Write a program that calculates the amount paid by each, if it is known that Y has paid 35% of the total, and Z has paid more than 15% Y. Consider two integers 2. Write a program that assigns the variable T Logical True value if the number of data are equal, otherwise False Value. 3 Consider two integers. The first number to be displayed, if it is greater than the second, otherwise both numbers to be displayed. . . Thanks a lot. ! Waiting Rs. Plizzz HELP ME!

cornel