0

Hello, I'll get straight to the point. I'm making a program which lets you enter a row of characters, finds words in that row (words are characters separated by spaces) and output them in another row in alphabetical order.

Firstly I wrote this:

Program ...;
         
Uses crt,sysutils;
         
const MAX = 100;

type chars = array[1..MAX] of char;
       strarray = array[1..MAX] of string;

var   symb:chars;
        astring:strarray;
        d,i,j,s,n:integer;

procedure Input;
    begin
        n:=0;
            while not Eoln and (n < MAX)do                                        
                begin
                    n := n + 1;
                    Read(symb[n]);
                end;
            readln;
            writeln;
     end;

Ok, so basically I enter a number of characters and attach them to a character array.The following code should divide the row of characters.
(For example:
>Input<
Have a nice day
>Output<
Have
a
nice
day

procedure words;
        begin
            i:=1;
            while i<n+1 do
                begin
                    if symb[i]<>' '
                        then Read(astring[i]);
                        else WriteLn;
                        WriteLn(str[i]);
                        inc(i);
                end;
        end;

I can do the dividing part by replacing

Read(astring[i]);

with

Write(symb[i]);

but I also want to want to attach those divided words to a string array like this:
(1st element)Have
(2nd)a
(3rd)nice
(4th)day

That's because I think of using AnsiCompareText to compare those words in a string array except I have no idea how to compare a string array with the said function:
This is a rough example of the ACT function:

procedure sort (s1,s2:string);
var r:longint;

begin
    R:=AnsiCompareText(s1,s2);
    If r<0
        then
            Write(s2,s1)
            else
    If r>0
        then
            Write(s1,s2)
            else
            Write(s1,s2);
end;

Help as well as different suggestions appreciated.
If the above is confusing, then I shall write how the program should behave:

>Input<
Have a nice day
>Output<
a day Have nice

2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by Duoas
0

It looks to me like you are mixing too many types.

To read a row of characters just read in a string

var s: string;
begin
writeln( 'Please enter a sentence:' );
readln( s );
...

The words function should take some arguments instead of using globals:

type tStrArray = array[ 1..MAX ] of string;

// Split s into words. Store the words in sa. Return the number of words stored.
function words( s: string; var sa: tStrArray ): integer;
  var
    i:    integer;
    temp: string;
  begin
  result := 0;  // number of words found

  for i := 1 to length( s ) do
    if s[ i ] = ' '
      then begin  // add the found word to the list
           if temp = '' then continue;
           inc( result );
           ss[ result ] := temp;
           temp = ''
           end
      else temp = temp +s[ i ];  // still finding the word

  if temp <> ''
    then begin
         inc( result );
         ss[ result ] := temp  // don't forget the last word...
         end
  end;

So now, you can say:

const
  MAX = 100;
type
  tStrArray = array[ 1..MAX ] of string;
var
  s: string;
  strarray: tStrArray;
  number_of_words: integer;
begin
writeln( 'Please enter a sentence:' );
readln( s );
number_of_words = words( s, strarray );
...

You'll have to continue from here. Next sort your list of strings. A bubble sort ought to do. Then use a loop to print your list of strings.

If this is homework, and if you are allowed, use tStringList (defined in the Classes unit) instead of an array of strings. It has a sort function built-in.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.