How to Count total number of Words in PDF?

How would you write a program to count the number of words beginning with 's'?

  • Hello, I need to create a program (very basic program using a string) to see how many words begin with s. No arrays or anything, just a basic program. This is what I have so far   var    i:integer;    count_beginwiths:integer;    sentence:string; begin   write('Type sentence, then hit enter (lowercase only): ');   readln(sentence);   count_beginwiths:=0;   for i:=1 to length(sentence) do   if sentence[i]=' s' then               <-- typing ' 'space' s' doesn't work :(   inc(count_beginwiths);   writeln('There were ', count_beginwiths, ' words beginning with s') ;   readln; end.

  • Answer:

    As already mentioned in an answer, if possible, use regular expressions to achieve this. Otherwise, if you want to do this with your current approach, you could modify the if statement in your program to be something like this instead: if sentence[i]==' ' AND sentence[i+1]=='s' then inc(count_beginwiths); There are two other alterations this will require. First, since we are actually checking the (i+1)the element, the loop needs to (and should) only run till length(sentence) - 1 Secondly, the algorithm assumes that 'words' are always preceded by spaces. Even if you don't want to handle cases where there may be punctuation marks preceding the s, you do want to handle the special case when the very first letter of the sentence is an 's', with something like if(sentence[1]=='s') then inc(count_beginwiths); Note: I'm not sure what language you're coding in, but are you sure the character array indices don't begin from 0?

Eeshan Malhotra at Quora Visit the source

Was this solution helpful to you?

Other answers

You should define what determines the starting character of a word. It seems that if it's a non-alphabetic character then you treat it as the starting point of the word. If that is the case, try the following code: //=========== var    i:integer;    count_beginwiths:integer;    sentence:string; begin   write('Type sentence, then hit enter (lowercase only): ');   readln(sentence);   count_beginwiths:=0;   // Special check for the 1st word only   if sentence[1] = 's' then   begin     inc(count_beginwiths);   end;   for i:=2 to length(sentence) do     if ((sentence[i] = 's') and (not(sentence[i-1] in ['a'..'z', 'A'..'Z']))) then     begin       inc(count_beginwiths);     end;   writeln('There were ', count_beginwiths, ' words beginning with s') ;   readln; end. //===========

Sang Do

In general you would use a regex (regular expression) for this and return the match count. There are some sites out there where you can play with this online. (This assumes that pascal has a regex library, haven't done pascal stuff in 20 years :-))

Martin Wawrusch

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.