Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading longest word within a string.

pascal string

  • Please log in to reply
6 replies to this topic

#1 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 April 2010 - 08:32 AM

Hey guys, I need help developing a simple program in Pascal where the user is prompted to enter a string and the program must identify the longest word within that sentence.

Here's the basic algorithm:
1) A password must be entered to use the program.
2) The user must enter a string. Once the string is entered, the program will check for a " / " to terminate the input procedure.
3) The string is analyzed letter by letter using the "read" command. The counter_temp is incremented by one unit. It checks for any spaces entered to distinguish different words within the phrase.
4) The program displays the number of chars contained in the longest word.

Variables:
character: letter entered by the user.
phrase: full string.
counter_temp: Temp storage of phrase. It is incremented every time it reads a character. It is reset to 0 once the user enters a space (marking the beginning of a new word).
counter: It stores the counter_temp quantity once a space is detected. It checks to see if it's larger than counter_temp to later store it as counter_max.
counter_max: It's where the longest word-quantity is stored. It displays the most amount of letters within the longest word.
pass: The program's password.

Here's the code:
procedure Analize_Phrase(var counter, counter_temp, counter_max : byte; character : char; phrase : string);
Begin
        If character = ' ' Then
        Begin
                    If (counter > counter_temp) Then
                       Begin
                        counter_max := counter;
                        counter_temp := 0
                       End
                    Else
                        counter := counter_temp;
                        counter_max := counter;
                        counter_temp := 0;
            End;
        If counter_temp <> 0 Then
                counter_max := counter_temp;
End;
procedure Main_Program(var counter, counter_temp, counter_max : byte; var phrase : string; var character : char);
Begin
    Writeln('Enter the string:');
    While (character <> '/') Do
        Begin
            read(character);
            phrase := phrase + character;
            inc(counter_temp);
            Analize_Phrase(counter, counter_temp, counter_max, character, phrase);
        End;
        writeln('The longest word entered contains: ',conter_max, ' letters');
        readln();
End;
procedure Intro(pass : string);
 var counter, counter_temp, counter_max : byte; character : char; phrase : string;
Begin
        counter_temp := 0; counter :=0; counter_max := 0;
        writeln('Enter the password:');
        readln(pass);
        If pass = '7540' then
        Main_Program(counter, counter_temp, counter_max, character, phrase)
        Else
                writeln('Wrong password...');
End;

var pass : string;
Begin
        Intro(pass);
        readln();
End.

The problem is that for some reason it does not read the longest word. It just displays the amount of letters contained in the last word entered. How do I fix it???

Thanks a lot!
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 April 2010 - 10:40 AM

It looks like your indentation was misleading you.

procedure Analize_Phrase(var counter, counter_temp, counter_max : byte; character : char; phrase : string);
Begin
  If character = ' ' Then
  Begin
    If (counter > counter_temp) Then
    Begin
      counter_max := counter;
      counter_temp := 0
    End
    Else
      counter := counter_temp;//this line should probably be in a begin/end block
    counter_max := counter;   //this line should probably be in a begin/end block
    counter_temp := 0;        //this line should probably be in a begin/end block
  End;
  If counter_temp <> 0 Then
    counter_max := counter_temp;
End;

procedure Main_Program(var counter, counter_temp, counter_max : byte; var phrase : string; var character : char);
Begin
  Writeln('Enter the string:');
  While (character <> '/') Do
  Begin
    read(character);
    phrase := phrase + character;
    inc(counter_temp);
    Analize_Phrase(counter, counter_temp, counter_max, character, phrase);
  End;
  writeln('The longest word entered contains: ',conter_max, ' letters');
  readln();
End;

procedure Intro(pass : string);
var 
  counter, counter_temp, counter_max : byte; character : char; phrase : string;
Begin
  counter_temp := 0; counter :=0; counter_max := 0;
  writeln('Enter the password:');
  readln(pass);
  If pass = '7540' then
    Main_Program(counter, counter_temp, counter_max, character, phrase)
  Else
    writeln('Wrong password...');
End;

var 
  pass : string;
Begin
  Intro(pass);
  readln();
End.

  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 psycho

psycho

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 April 2010 - 11:07 PM

Hey WingedPanther,
Thanks for helping me out. I've updated the code to this, inside the procedure:
 Begin 
                        counter := counter_temp;
                        counter_max := counter;
                        counter_temp := 0;
End;
But it still won't work! Is my algorithm totally invalid?
Thanks again!
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 April 2010 - 03:09 PM

Let's start with this: what do counter, counter_max, and counter_temp each represent?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 18 April 2010 - 05:43 PM

program FindWord;
 
var c:Char;
     cur,max:integer;
    pass:string;
begin
 write('Password please:'); readln(pass); writeln;
 if pass<>'7540' then
  begin
   writeln('Sorry, the password is "7540" and that''s not what you entered. You''ll have to run this program again. Security is important to us.');
   writeln('If you want to change the password, you have to find the source to this program and edit it, the password is hard-coded. Once someone knows it and shouldn''t');
   writeln('you have a breach of security you can''t fix unless you have the source and a compiler and programming knowledge. Good luck.');
   exit; //you can also use halt;
  end;
 c:=#0;
 cur:=0;
 max:=0;
 while c<>'/' do
  begin
   read(c);
   if c=' ' then //end of word?
     begin
      if cur>max then max:=cur; // if the current word (last word) is longer than max word, max is now the current word. I think THIS is where you messed up...
      cur:=0; //start counting from 0 again...
     end
   else
    if c<>'/' then //DONT'T count the last closing slash
     begin
      inc(cur); //this is a regular word char, so count it.
     end;
  end;
 writeln('Well, the longest word is ',max,' chars long.');
end.
 

I'm pretty sure your program doesn't compare the "current" to the "Max" to find out if the new one is bigger. Also, you are passing all params as VAR and that's a big no-no.
As you can see, you're making it a lot harder than you ought to. Maybe your teacher said you must use procedures or functions... Just shove the real stuff in a function that returns MAX (move vars to the function, don't use var params!).

Anyway, good luck.
  • 0

#6 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 18 April 2010 - 05:45 PM

One thing I forgot is that I believe the "//" line comments don't work in TP. So erase and delete the comments, or it won't compile. I forget though, it's been about a decade...

By the way, you CAN, even on good ol' TP, use DEBUGGING TOOLS to see exactly what's going on. Then you'll quickly realize that what you want it to do isn't what it's doing, and you'll see WHY really fast. But knowing it's wrong might not help you find out how to do it right...
  • 0

#7 nidal

nidal

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 07 April 2016 - 09:48 AM

hi its my first post on this site i wish that i am welcome here

this is a definition of a function that return the longest word in a string.

function maxword(ch:string):integer;
var n,nmax:integer;
begin
   n:=0;
   nmax:=0;
  for i:=1 to length(ch) do
  begin

   if (upcase(ch[i]) in ['A'..'Z'])then 
{we check if the char is a letter then increment n}
   n:=n+1
   else if (n>nmax)then 
{in case of the char is not a letter we initilise the counter n to 0 but before we check if is n>nmax}
   begin
   nmax:=n;
   n:=0;
   end
   else
{if is n<nmax and ch[i] not in A..Z then just initilise n to 0} 
   n:=0;

  end;
  {till that we can do maxword:=nmax and the function work fine but in particular case
   when the longest word is the last word or the string don't have a delimiter the execution
   will finish with n = a value and nmax = other value or 0(in case we don't have delimeters at all)
   and the last value of n will not compared to the previous value of n so we add those line
   ex 1
   ch:='hi i like fruits'; maxword will finish with n=6 and nmax = 0;
   
   ex 2 
   ch:='understand';maxword will finish with n=10 and nmax = 0;
}
  if n<nmax then
  maxword:=nmax
  else
  maxword:=n;

end;

{2}


  • 0