Find the shortest word in the user-entered line (in the delpas).

  • 7
    fi, as trite @BenderT, try it yourself, that will not work - I will help. No one will write for you. - Sh4dow
  • Yes, trite, but damn, it was this example that didn’t work out something, although what’s funny is that much more complex examples (including complex ones from OOP) turned out to be correct))) - BenderT

4 answers 4

Get the string.

Set up four variables:

  1. Counter - the length of the current word;
  2. Position of the beginning of the current word;
  3. The length of the shortest word encountered;
  4. Position of the beginning of the shortest word encountered.

We start in the loop to pass the string character by character. For fixed-length character encodings (a la CP1251, CP866 or ISO-8859-1), everything is simple; for variable-length character encodings (for example, UTF-8), you need to be more cunning. Shkolote and non-core and / or young students, as a rule, do not talk about these difficulties, so if there is no understanding of what it is about, then you can ignore the paragraph. Until.

If the next symbol is a letter (a letter or another, a symbol included in the “word”), then:

  • If the current word length counter was 0 (no current word), then remember the position.
  • Make +1 to the length of the current word.

If not litera:

  • If the length of the current word is greater than 0 (i.e. there was something), then compare it with the length of the shortest word encountered.
  • Found shorter? Remember the length and position.
  • We reset the length of the current word to 0.

So we drive to the end of the line, or until the word is found in 1 character (i.e. there is no further search for it).

Next thing is the technique - the position in the string we know, the length of the word (or the fact that there were no words in the string) - we know.

No arrays and other circus is needed here. It is possible, but not necessary. Dumb state machine, search in one pass, for O(n) .

Here is the whole basic idea. Code - please write yourself, in the end this is your task. If something is not clear, ask specific questions (and how you tried to solve them).

  • I repent, a plus sign ) I had to think about it) - Sh4dow
  • Well, at the same time to such a detailed description, I would hint for beginners how to look at the first UTF-8 bytic to calculate how many bytes this character takes (therefore, how many bytes to move further). - avp
  • one
    Well, if on the fingers, then for UTF-8 the rule is simple - calculate how many higher single bits. How many bits, so many bytes, and takes a character, except that if the high bit is zero, then the character takes one (this) byte. Those. if the first byte, say, 0xC2 = 0b * 11 * 000010 , then the character takes 2 bytes (i.e., further one more). Algorithms parsing UTF-8 in the network heap. - drdaeman
  • If we assume that utf is not broken, then it is not necessary to read all the bytes inside the utf-symbol, since in punctuation characters (as well as numbers, etc.), the most significant bit is 0. Although, it all depends on the rules, which character is part of a word. - avp
  • If the most significant bit is zero, then we have read the whole character, there is nothing to skip;) And then - if we talk about Unicode - then do it humanly, using the Unicode Character Database . Apparently, not with my hands (too ungrateful), but I absolutely do not know how to work with libicu in Delphi. - drdaeman

The algorithm is about the same (very simple, you can certainly optimize):

  1. Assign the sentence to a string variable.
  2. We break the sentence into words and store it in an array. For example, by space, comma, etc.
  3. We get a variable and assign it the length of the first word ( minLenght , for example). We also set a variable to store the index.
  4. Then in the cycle we run through the array of words and compare the length of the word with the variable that we have ( minLength ). If the length is less, then assign this variable the length of the current word.
  5. Print the smallest word, know the length and know the index in the array.
  • Why break and create a copy in an array? Excess memory to work out? Here we need, in fact, the simplest finite state machine, passing through the string character by character. - drdaeman 1:59 pm
  • The task is trivial, but if the author sets, then his knowledge is not very deep yet, so I decided to write like this. If the author writes at least such an option (himself), then in the future he will be able to optimize the solution himself, think about the memory and the other. - sharok
  • from memory you can fit like this: var userString, minWord: string; pos, wordStart: integer; readingWord: boolean; @sharok, it's harder to break into an array) - Sh4dow
  • @ Sh4dow: minWord - ugly. Again, the extra copy until we find really the shortest word. - drdaeman

I, of course, in Delphi is not strong, but these are the basics! Want to achieve a result - memorize the basics. The syntax of the language is not so important. The point is this: it is impossible to predict in advance what the user will enter. He can create a string with just one word. And maybe four to try to dial. A string is, in any case, an array of characters. Consider the concept of "word" in the context of the compiler: the word is a sequence of characters, separated by spaces. It is us, people, who take words as something meaningful, which allows us to express our thoughts. However, for computer technology it is not. I will give an example: рпавол вфушщо мтвлопопра . If you take roughly - it is a line of three words and it does not matter that they do not make sense.

Well, here we have an understanding of the essence of the problem. We make two counters: one to determine the continuous sequence of characters (the length of the word differently), the other to memorize the number of the longest word. That's all. We take an array of these characters (the string entered by the user) and loop. Count the number of characters other than a space, meet a space - save the number in an array variable that stores word lengths, etc. At the output, we obtain an array in which the lengths of the words are located, of which we must find the maximum and display it on the screen. Well, with the conclusion of the number of the longest word, I think, there will be no problems.

    Here is the solution to this problem using OOP:

     program Project3; {$APPTYPE CONSOLE} uses Classes; type TLst = class(TStringList) function CompareStrings(const a,b: string): integer; override; end; function TLst.CompareStrings; begin Result := Length(a)-Length(b); if Result = 0 then Result := inherited CompareStrings(a,b) end; var s : string; begin with TLst.Create do try Sorted := true; repeat ReadLn(s); DelimitedText := s; if Count > 0 then WriteLn('<',Strings[0],'>') until s = '' finally Free end end. 

    Moreover, an arbitrary number of lines is processed (in the loop repeat ReadLn (s) until the empty line is entered - until s = ''). Thus, the main body of the Repeat loop consists of only two operators:

     DelimitedText := s; // - очередную строку разбить на слова и отсортировать WriteLn(Strings[0]); // - верхний элемент получившегося списка и есть ответ 

    Where the leading statement is if if Count> 0 then ... is "protection" from an empty list.

    The fact that the split list of words will be sortable is determined by assigning the Sorted: = true property, and the correct sorting for our task is established by redefining the CompareStrings function (in accordance with the spirit and the OOP letter) in the heir of TStringList , which we used as the basis:

    "... A robe is the same, but the buttons are nacreous ..." (c) c / f The diamond hand.

    It should be noted that the sorting set here is used even more sophisticated than indicated in the original task, since it does not indicate what to do if the initial line contains several words with a minimum length - who should I choose? Therefore, there is a sorting according to a double feature: each comparison goes first along the length of the words, and then, with an equal length, alphabetically, i.e. traditional lexicographical order is used (due to the use of inherited ... ).

    If desired, if it is necessary to distinguish between upper and lower case letters (by default, they do not differ here), simply assign the corresponding value to the CaseSensitive property.

    I would especially like to note that here an anonymous list is used (developed by us when inheriting TStringList - TLst ) without using a variable of this type in the var section and therefore (using the with operator) all statements inside a protected try-finally-end block are short and descriptive , and the program itself is understandable, transparent and easily modified.

    • As a result, we have a godless memory, and probably the slowest of programs that solve the same problem. However, it seems, the OOP adepts do not care for a long time. - avp pm
    • Show class? - its alternative program, which is also easily modified under the newly emerging circumstances in the task and at the same time not so "... godlessly devouring memory ..." and such "... the slowest of programs ..." We compare them: we will see their size, memory consumption and execution speed. Let us estimate what we gain and what we lose and at what price, and then we draw conclusions - how right or wrong are those who are the “adept of the PLO” and why, perhaps, they do not care ... - Barklay
    • Maybe the price of the issue - the balance of losses and benefits - is greatly exaggerated towards losses in the heads of some "experts", but in reality everything is quite the opposite, and perhaps this explains the fact of the worldwide recognition of OOP technologies? - Barklay
    • Essentially somewhere (on crosses) ifstream inpf (FILENAME); string s, w; size_t len ​​= 0, curlen; while (getword (inpf, s)) {curlen = count_chars (s); if (! len || curlen> len) {w = s; len = curlen; }} The rest is obvious. - avp
    • @avp> As a result, we have a godlessly gnawing memory, and probably the slowest of programs that solve the same problem. However, it seems, the OOP adepts do not care for a long time. -_- - etki