Find longest word in string
Here given code implementation process.
// Java program
// Find longest word in string
class MyString
{
//Find the longest word in a string
public String longest_word(String str_data)
{
String auxiliary = "";
String result = "";
//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.length())
{
result = auxiliary;
}
i = j;
}
}
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
// Simple case
String result = obj.longest_word("This is wrong logic");
System.out.print(" Longest Word : " + result + "\n");
result = obj.longest_word("This is very dangerous error");
System.out.print(" Longest Word : " + result + "\n");
result = obj.longest_word("Seriously it is true ");
System.out.print(" Longest Word : " + result + "\n");
}
}
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
// C++ program
// Find longest word in string
//Include header file
#include <iostream>
using namespace std;
class MyString
{
public:
//Find the longest word in a string
string longest_word(string str_data)
{
string auxiliary = "";
string result = "";
//Loop contolling variables
int i = 0, j = 0;
cout << "\nGiven 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.size())
{
result = auxiliary;
}
i = j;
}
}
return result;
}
};
int main()
{
MyString obj = MyString();
// Simple case
string result = obj.longest_word("This is wrong logic");
cout << " Longest Word : " << result << "\n";
result = obj.longest_word("This is very dangerous error");
cout << " Longest Word : " << result << "\n";
result = obj.longest_word("Seriously it is true ");
cout << " Longest Word : " << result << "\n";
return 0;
}
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
// C# program
// Find longest word in string
//Include namespace system
using System;
class MyString
{
//Find the longest word in a string
public String longest_word(String str_data)
{
String auxiliary = "";
String result = "";
//Loop contolling variables
int i = 0, j = 0;
Console.Write("\nGiven 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.Length)
{
result = auxiliary;
}
i = j;
}
}
return result;
}
public static void Main(String[] args)
{
MyString obj = new MyString();
// Simple case
String result = obj.longest_word("This is wrong logic");
Console.Write(" Longest Word : " + result + "\n");
result = obj.longest_word("This is very dangerous error");
Console.Write(" Longest Word : " + result + "\n");
result = obj.longest_word("Seriously it is true ");
Console.Write(" Longest Word : " + result + "\n");
}
}
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
<?php
// Php program
// Find longest word in string
class MyString
{
//Find the longest word in a string
public function longest_word($str_data)
{
$auxiliary = "";
$result = "";
//Loop contolling variables
$i = 0;
$j = 0;
echo "\nGiven 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) > strlen($result))
{
$result = $auxiliary;
}
$i = $j;
}
}
return $result;
}
}
function main()
{
$obj = new MyString();
// Simple case
$result = $obj->longest_word("This is wrong logic");
echo " Longest Word : ". $result ."\n";
$result = $obj->longest_word("This is very dangerous error");
echo " Longest Word : ". $result ."\n";
$result = $obj->longest_word("Seriously it is true ");
echo " Longest Word : ". $result ."\n";
}
main();
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
// Node Js program
// Find longest word in string
class MyString
{
//Find the longest word in a string
longest_word(str_data)
{
var auxiliary = "";
var result = "";
//Loop contolling variables
var i = 0;
var j = 0;
process.stdout.write("\nGiven 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.length)
{
result = auxiliary;
}
i = j;
}
}
return result;
}
}
function main()
{
var obj = new MyString();
// Simple case
var result = obj.longest_word("This is wrong logic");
process.stdout.write(" Longest Word : " + result + "\n");
result = obj.longest_word("This is very dangerous error");
process.stdout.write(" Longest Word : " + result + "\n");
result = obj.longest_word("Seriously it is true ");
process.stdout.write(" Longest Word : " + result + "\n");
}
main();
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
# Python 3 program
# Find longest word in string
class MyString :
# Find the longest word in a string
def longest_word(self, str_data) :
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\nGiven 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) > len(result)) :
result = auxiliary
i = j
i += 1
return result
def main() :
obj = MyString()
# Simple case
result = obj.longest_word("This is wrong logic")
print(" Longest Word : ", result ,"\n", end = "")
result = obj.longest_word("This is very dangerous error")
print(" Longest Word : ", result ,"\n", end = "")
result = obj.longest_word("Seriously it is true ")
print(" Longest Word : ", result ,"\n", end = "")
if __name__ == "__main__": main()
Output
Given String : [ This is wrong logic ]
Longest Word : wrong
Given String : [ This is very dangerous error ]
Longest Word : dangerous
Given String : [ Seriously it is true ]
Longest Word : Seriously
# Ruby program
# Find longest word in string
class MyString
# Find the longest word in a string
def longest_word(str_data)
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\nGiven 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.length())
result = auxiliary
end
i = j
end
i += 1
end
return result
end
end
def main()
obj = MyString.new()
# Simple case
result = obj.longest_word("This is wrong logic")
print(" Longest Word : ", result ,"\n")
result = obj.longest_word("This is very dangerous error")
print(" Longest Word : ", result ,"\n")
result = obj.longest_word("Seriously it is true ")
print(" Longest Word : ", result ,"\n")
end
main()
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
import scala.util.control.Breaks._
// Scala program
// Find longest word in string
class MyString
{
//Find the longest word in a string
def longest_word(str_data: String): String = {
var auxiliary: String = "";
var result: String = "";
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\nGiven 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.length())
{
result = auxiliary;
}
i = j;
}
i += 1;
}
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
// Simple case
var result: String = obj.longest_word("This is wrong logic");
print(" Longest Word : " + result + "\n");
result = obj.longest_word("This is very dangerous error");
print(" Longest Word : " + result + "\n");
result = obj.longest_word("Seriously it is true ");
print(" Longest Word : " + result + "\n");
}
}
Output
Given String : [This is wrong logic]
Longest Word : wrong
Given String : [This is very dangerous error]
Longest Word : dangerous
Given String : [Seriously it is true ]
Longest Word : Seriously
// Swift program
// Find longest word in string
class MyString
{
//Find the longest word in a string
func longest_word(_ text: String) -> String
{
var auxiliary: String = "";
var result: String = "";
var str_data = Array(text);
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\nGiven String : [", text ,"]\n", terminator: "");
//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.count)
{
result = auxiliary;
}
i = j;
}
i += 1;
}
return result;
}
}
func main()
{
let obj: MyString = MyString();
// Simple case
var result: String = obj.longest_word("This is wrong logic");
print(" Longest Word : ", result ,"\n", terminator: "");
result = obj.longest_word("This is very dangerous error");
print(" Longest Word : ", result ,"\n", terminator: "");
result = obj.longest_word("Seriously it is true ");
print(" Longest Word : ", result ,"\n", terminator: "");
}
main();
Output
Given String : [ This is wrong logic ]
Longest Word : wrong
Given String : [ This is very dangerous error ]
Longest Word : dangerous
Given String : [ Seriously it is true ]
Longest Word : Seriously
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