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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Specific Words within a String

Status
Not open for further replies.

furthur

Technical User
Nov 1, 2000
9
0
0
US
I am needing to count how many times a specific word appears within a string. For example, if a string is

"Complaint, Question, Complaint, Praise"

then I need a formula that can tell me that the word "Complaint" appears twice. The string will be comma separated, and could have anywhere from one to ten components.
 
Here is a first attempt to solve your problem. It passes the syntax check but there is an error in it. My wife is calling me for super so I may not have time to de-bug it tonight. We are going to see Harry Potter, after dinner.
So I am counting on my good friends to fix my errors. Great question!

WhilePrintingRecords;
stringVar array words;
numbervar k := 1;
numbervar m := 1;
numbervar n := 1;
numbervar p := 1;
stringvar t := "";
numbervar kount := 0;

for n := 1 to length(trim({@input})) Do
(
for p := n to InStr (n,{@input} ,", ") -1 Do
(
words[k] := words[k] + {@input}[n]
);
k := k +1;
n := n + 2;
);

for n := 1 to k Do
(
if words[n] = "complaint" then kount := kount + 1
);
kount
Howard Hammerman,

Crystal Reports training, consulting, books, training material, software, and support. Scheduled training in 8 cities.
howard@hammerman.com
800-783-2269
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top