Home c++ Search for the longest word in the string. C++

Search for the longest word in the string. C++

Author

Date

Category

You need to find the longest word in the string. Bestly! Without a String class! The problem is that the code does not work. And I displays the whole line instead of the longest word

#include & lt; iostream & gt;
  #Include & lt; Locale & gt;
  #Include & lt; CString & GT;
  Using Namespace STD;
INT MAIN ()
{
  SETLOCALE (LC_ALL, "RUS");
  Char S [100];
  INT N, I, K, KOL, MAX = 0, Y, G, J = 0;
  CIN.GetLine (S, 100);
  int p;
  i = 0;
  For (int l = 0; L & lt; strlen (s); l ++) // We pass along the length of the line, and we are looking for spaces, and after everyone we measuring the word.
  {
  While (s [L]! = '')
  {
    if (s [l] == '')
    {
      P = L;
      L ++;
    }
    ELSE.
    {
      do.
      {
        L ++;
      } While (s [l]! = ''); // Word length
       kol = strlen (s) -l-p; // Word Length // P Last Space
      if (KOL & GT; MAX)
      {
        L ++;
           /// Max Word.
        Max = KOL;
      }
    }
   }
  }
  for (n; n & lt; k; n ++)
  {
    COUT & LT; & LT; S [n];
  }
  COUT & LT; & LT; "- the longest word" & lt; & lt; Endl;
  SYSTEM ("PAUSE");
}

Answer 1, Authority 100%

You have a difficult taste …
I would do this:

int main ()
{
  Const int strlen = 200;
  Char s [strlen];
  CIN.GetLine (S, Strlen);
  Size_t maxlen = 0;
  char * maxidx = nullptr;
  for (char * c = s; * c;)
  {
    While (* C == '') ++ C;
    if (* C == 0) Break;
    char * begin = c;
    while (* C & amp; & amp; * c! = '') ++ C;
    If (Maxlen & LT; (C - Begin))
    {
      Maxlen = C - Begin;
      maxidx = begin;
    }
  }
  if (maxlen == 0)
  {
    COUT & LT; & LT; "Empty Line! \ N";
    Return 0;
  }
  ELSE.
  {
    * (Maxidx + maxlen) = 0;
    COUT & LT; & LT; Maxidx;
  }
}

Answer 2

Not sure that this will help you, here is a working code:

# include & lt; iostream & gt;
Using Namespace STD;
INT MAIN () {
  // Stork
  CHAR * THESTRING = "DSJFKD1 SDF2 DSFKDSJ3 SDFJSDKJFDSJF4 D5";
  char * theletter = trestring; // Here we will store the address of the next symbol since the first
  unsigned counter = 0, // counting counter in the word
    Storage = 0; // Here we will store the number of letters of the biggest word
  While (* Theletter) {// repeat until it comes the zero symbol of the end
    Theletter ++; // Increase the following address to the size of the symbol
    if (* theletter! = '') // if not space
      Counter ++; // Increase the counter
    ELSE.
      if (counter & gt; storage) {// if letters more than we knew before,
        Storage = Counter; // then save this number
        Counter = 0; // Agotum reset the counting counter
      }
  }
  COUT & LT; & LT; Storage & LT; & lt; Endl; // result
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions