Jump to content

Reading longest word within a string.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
psycho

psycho

    Newbie

  • Members
  • PipPip
  • 11 posts
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!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.

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

#3
psycho

psycho

    Newbie

  • Members
  • PipPip
  • 11 posts
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!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Let's start with this: what do counter, counter_max, and counter_temp each represent?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
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.

#6
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
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...