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?
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.
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.
(S) is your input string;
- Create a for loop that goes from 1 to the length of (S) - 1.
- 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
- 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.
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?
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!
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!