Posted on by Kalkicode
Code String

Find the length of longest word in string

Here given code implementation process.

// Java program
// Find longest word length in string
class MyString
{
  //Find the longest word in a string
  public int longest_word_length(String str_data)
  {
    String auxiliary = "";
    int result = 0;
    //Loop contolling variables
    int i = 0, j = 0;
    System.out.print("\nGiven String : [" + str_data + "]\n");
    //Loop which is iterate given string character elements
    for (i = 0; i < str_data.length(); i++)
    {
      if (str_data.charAt(i) != ' ')
      {
        //reset variable
        auxiliary = "";
        //loop, combine string text
        for (j = i; j < str_data.length(); j++)
        {
          //Check that given character is space character or not
          if (str_data.charAt(j) != ' ')
          {
            //When not get space
            auxiliary = auxiliary + str_data.charAt(j);
          }
          else
          {
            //When find space break this loop 
            break;
          }
        }
        if (auxiliary.length() > result)
        {
          result = auxiliary.length();
        }
        i = j;
      }
    }
    return result;
  }
  public static void main(String[] args)
  {
    MyString obj = new MyString();
    // Simple case
    int length = obj.longest_word_length("You are wrong");
    System.out.print(" Length of longest word is : " + length + "\n");
    length = obj.longest_word_length("This is an genetic problems we cannot handle this");
    System.out.print(" Length of longest word is  : " + length + "\n");
    //When given string contains space
    length = obj.longest_word_length("   ");
    System.out.print(" Length of longest word is  : " + length + "\n");
  }
}

Output

Given String : [You are wrong]
 Length of longest word is : 5

Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

Given String : [   ]
 Length of longest word is  : 0
// C++ program
// Find longest word length in string

//Include header file
#include <iostream>

using namespace std;
class MyString
{
  public:
    //Find the longest word in a string
    int longest_word_length(string str_data)
    {
      string auxiliary = "";
      int result = 0;
      //Loop contolling variables
      int i = 0, j = 0;
      cout << "\n Given String : [" << str_data << "]\n";
      //Loop which is iterate given string character elements
      for (i = 0; i < str_data.size(); i++)
      {
        if (str_data[i] != ' ')
        {
          //reset variable
          auxiliary = "";
          //loop, combine string text
          for (j = i; j < str_data.size(); j++)
          {
            //Check that given character is space character or not
            if (str_data[j] != ' ')
            {
              //When not get space
              auxiliary = auxiliary + str_data[j];
            }
            else
            {
              //When find space break this loop 
              break;
            }
          }
          if (auxiliary.size() > result)
          {
            result = auxiliary.size();
          }
          i = j;
        }
      }
      return result;
    }
};
int main()
{
  MyString obj = MyString();
  // Simple case
  int length = obj.longest_word_length("You are wrong");
  cout << " Length of longest word is : " << length << "\n";
  length = obj.longest_word_length("This is an genetic problems we cannot handle this");
  cout << " Length of longest word is  : " << length << "\n";
  //When given string contains space
  length = obj.longest_word_length("   ");
  cout << " Length of longest word is  : " << length << "\n";
  return 0;
}

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
// C# program
// Find longest word length in string
//Include namespace system
using System;
class MyString
{
  //Find the longest word in a string
  public int longest_word_length(String str_data)
  {
    String auxiliary = "";
    int result = 0;
    //Loop contolling variables
    int i = 0, j = 0;
    Console.Write("\n Given String : [" + str_data + "]\n");
    //Loop which is iterate given string character elements
    for (i = 0; i < str_data.Length; i++)
    {
      if (str_data[i] != ' ')
      {
        //reset variable
        auxiliary = "";
        //loop, combine string text
        for (j = i; j < str_data.Length; j++)
        {
          //Check that given character is space character or not
          if (str_data[j] != ' ')
          {
            //When not get space
            auxiliary = auxiliary + str_data[j];
          }
          else
          {
            //When find space break this loop 
            break;
          }
        }
        if (auxiliary.Length > result)
        {
          result = auxiliary.Length;
        }
        i = j;
      }
    }
    return result;
  }
  public static void Main(String[] args)
  {
    MyString obj = new MyString();
    // Simple case
    int length = obj.longest_word_length("You are wrong");
    Console.Write(" Length of longest word is : " + length + "\n");
    length = obj.longest_word_length("This is an genetic problems we cannot handle this");
    Console.Write(" Length of longest word is  : " + length + "\n");
    //When given string contains space
    length = obj.longest_word_length("   ");
    Console.Write(" Length of longest word is  : " + length + "\n");
  }
}

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
<?php
// Php program
// Find longest word length in string
class MyString
{
  //Find the longest word in a string
  public  function longest_word_length($str_data)
  {
    $auxiliary = "";
    $result = 0;
    //Loop contolling variables
    $i = 0;
    $j = 0;
    echo "\n Given String : [". $str_data ."]\n";
    //Loop which is iterate given string character elements
    for ($i = 0; $i < strlen($str_data); $i++)
    {
      if ($str_data[$i] != ' ')
      {
        //reset variable
        $auxiliary = "";
        //loop, combine string text
        for ($j = $i; $j < strlen($str_data); $j++)
        {
          //Check that given character is space character or not
          if ($str_data[$j] != ' ')
          {
            //When not get space
            $auxiliary = $auxiliary . $str_data[$j];
          }
          else
          {
            //When find space break this loop 
            break;
          }
        }
        if (strlen($auxiliary) > $result)
        {
          $result = strlen($auxiliary);
        }
        $i = $j;
      }
    }
    return $result;
  }
}

function main()
{
  $obj = new MyString();
  // Simple case
  $length = $obj->longest_word_length("You are wrong");
  echo " Length of longest word is : ". $length ."\n";
  $length = $obj->longest_word_length("This is an genetic problems we cannot handle this");
  echo " Length of longest word is  : ". $length ."\n";
  //When given string contains space
  $length = $obj->longest_word_length("   ");
  echo " Length of longest word is  : ". $length ."\n";
}
main();

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
// Node Js program
// Find longest word length in string
class MyString
{
  //Find the longest word in a string
  longest_word_length(str_data)
  {
    var auxiliary = "";
    var result = 0;
    //Loop contolling variables
    var i = 0;
    var j = 0;
    process.stdout.write("\n Given String : [" + str_data + "]\n");
    //Loop which is iterate given string character elements
    for (i = 0; i < str_data.length; i++)
    {
      if (str_data[i] != ' ')
      {
        //reset variable
        auxiliary = "";
        //loop, combine string text
        for (j = i; j < str_data.length; j++)
        {
          //Check that given character is space character or not
          if (str_data[j] != ' ')
          {
            //When not get space
            auxiliary = auxiliary + str_data[j];
          }
          else
          {
            //When find space break this loop 
            break;
          }
        }
        if (auxiliary.length > result)
        {
          result = auxiliary.length;
        }
        i = j;
      }
    }
    return result;
  }
}

function main()
{
  var obj = new MyString();
  // Simple case
  var length = obj.longest_word_length("You are wrong");
  process.stdout.write(" Length of longest word is : " + length + "\n");
  length = obj.longest_word_length("This is an genetic problems we cannot handle this");
  process.stdout.write(" Length of longest word is  : " + length + "\n");
  //When given string contains space
  length = obj.longest_word_length("   ");
  process.stdout.write(" Length of longest word is  : " + length + "\n");
}
main();

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
#  Python 3 program
#  Find longest word length in string
class MyString :
  # Find the longest word in a string
  def longest_word_length(self, str_data) :
    auxiliary = ""
    result = 0
    # Loop contolling variables
    i = 0
    j = 0
    print("\n Given String : [", str_data ,"]\n", end = "")
    # Loop which is iterate given string character elements
    while (i < len(str_data)) :
      if (str_data[i] != ' ') :
        # reset variable
        auxiliary = ""
        # loop, combine string text
        j = i
        while (j < len(str_data)) :
          # Check that given character is space character or not
          if (str_data[j] != ' ') :
            # When not get space
            auxiliary = auxiliary + str_data[j]
          else :
            # When find space break this loop 
            break
          
          j += 1
        
        if (len(auxiliary) > result) :
          result = len(auxiliary)
        
        i = j
      
      i += 1
    
    return result
  

def main() :
  obj = MyString()
  #  Simple case
  length = obj.longest_word_length("You are wrong")
  print(" Length of longest word is : ", length ,"\n", end = "")
  length = obj.longest_word_length("This is an genetic problems we cannot handle this")
  print(" Length of longest word is  : ", length ,"\n", end = "")
  # When given string contains space
  length = obj.longest_word_length("   ")
  print(" Length of longest word is  : ", length ,"\n", end = "")

if __name__ == "__main__": main()

Output

 Given String : [ You are wrong ]
 Length of longest word is :  5

 Given String : [ This is an genetic problems we cannot handle this ]
 Length of longest word is  :  8

 Given String : [     ]
 Length of longest word is  :  0
#  Ruby program
#  Find longest word length in string
class MyString

  # Find the longest word in a string
  def longest_word_length(str_data)
  
    auxiliary = ""
    result = 0
    # Loop contolling variables
    i = 0
    j = 0
    print("\n Given String : [", str_data ,"]\n")
    # Loop which is iterate given string character elements
    while (i < str_data.length())
    
      if (str_data[i] != ' ')
      
        # reset variable
        auxiliary = ""
        # loop, combine string text
        j = i
        while (j < str_data.length())
        
          # Check that given character is space character or not
          if (str_data[j] != ' ')
          
            # When not get space
            auxiliary = auxiliary + str_data[j]
          else
          
            # When find space break this loop 
            break
          end
          j += 1
        end
        if (auxiliary.length() > result)
        
          result = auxiliary.length()
        end
        i = j
      end
      i += 1
    end
    return result
  end
end
def main()

  obj = MyString.new()
  #  Simple case
  length = obj.longest_word_length("You are wrong")
  print(" Length of longest word is : ", length ,"\n")
  length = obj.longest_word_length("This is an genetic problems we cannot handle this")
  print(" Length of longest word is  : ", length ,"\n")
  # When given string contains space
  length = obj.longest_word_length("   ")
  print(" Length of longest word is  : ", length ,"\n")
end
main()

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
import scala.util.control.Breaks._
// Scala program
// Find longest word length in string
class MyString
{
  //Find the longest word in a string
  def longest_word_length(str_data: String): Int = {
    var auxiliary: String = "";
    var result: Int = 0;
    //Loop contolling variables
    var i: Int = 0;
    var j: Int = 0;
    print("\n Given String : [" + str_data + "]\n");
    //Loop which is iterate given string character elements
    while (i < str_data.length())
    {
      if (str_data(i) != ' ')
      {
        //reset variable
        auxiliary = "";
        //loop, combine string text
        j = i;
                breakable {
        while (j < str_data.length())
        {
          //Check that given character is space character or not
          if (str_data(j) != ' ')
          {
            //When not get space
            auxiliary = auxiliary + str_data(j);
          }
          else
          {
            //When find space break this loop 
            break;
          }
          j += 1;
        }
                }
        if (auxiliary.length() > result)
        {
          result = auxiliary.length();
        }
        i = j;
      }
      i += 1;
    }
    return result;
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyString = new MyString();
    // Simple case
    var length: Int = obj.longest_word_length("You are wrong");
    print(" Length of longest word is : " + length + "\n");
    length = obj.longest_word_length("This is an genetic problems we cannot handle this");
    print(" Length of longest word is  : " + length + "\n");
    //When given string contains space
    length = obj.longest_word_length("   ");
    print(" Length of longest word is  : " + length + "\n");
  }
}

Output

 Given String : [You are wrong]
 Length of longest word is : 5

 Given String : [This is an genetic problems we cannot handle this]
 Length of longest word is  : 8

 Given String : [   ]
 Length of longest word is  : 0
// Swift program
// Find longest word length in string
class MyString
{
  //Find the longest word in a string
  func longest_word_length(_ text: String) -> Int
  {
        let str_data = Array(text);
    var auxiliary: String = "";
    var result: Int = 0;
    //Loop contolling variables
    var i: Int = 0;
    var j: Int = 0;
    print("\n Given String : [", text ,"]");
    //Loop which is iterate given string character elements
    while (i < str_data.count)
    {
      if (str_data[i] != " ")
      {
        //reset variable
        auxiliary = "";
        //loop, combine string text
        j = i;
        while (j < str_data.count)
        {
          //Check that given character is space character or not
          if (str_data[j] != " ")
          {
            //When not get space
            auxiliary = auxiliary + String(str_data[j]);
          }
          else
          {
            //When find space break this loop 
            break;
          }
          j += 1;
        }
        if (auxiliary.count > result)
        {
          result = auxiliary.count;
        }
        i = j;
      }
      i += 1;
    }
    return result;
  }
}
func main()
{
  let obj: MyString = MyString();
  // Simple case
  var length: Int = obj.longest_word_length("You are wrong");
  print(" Length of longest word is : ", length ,"\n", terminator: "");
  length = obj.longest_word_length("This is an genetic problems we cannot handle this");
  print(" Length of longest word is  : ", length ,"\n", terminator: "");
  //When given string contains space
  length = obj.longest_word_length("   ");
  print(" Length of longest word is  : ", length ,"\n", terminator: "");
}
main();

Output

 Given String : [ You are wrong ]
 Length of longest word is :  5

 Given String : [ This is an genetic problems we cannot handle this ]
 Length of longest word is  :  8

 Given String : [     ]
 Length of longest word is  :  0

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment