Reverse words in a given string
Here given code implementation process.
// C++ program
// Reverse words in a given string
//Include header file
#include <iostream>
using namespace std;
class MyString
{
public:
//Reverse words in given text from end to beginning
string reverse_words(string str)
{
string auxiliary = "";
string result = "";
//Loop contolling variables
int i = 0, j = 0;
cout << "\nGiven String : [" << str << "]\n";
//Loop which is iterate given string character elements
for (i = 0; i < str.size(); i++)
{
if (str[i] != ' ')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str.size(); j++)
{
//Check that given character is space character or not
if (str[j] != ' ')
{
//When not get space
auxiliary = auxiliary + str[j];
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
}
return result;
}
};
int main()
{
MyString obj = MyString();
string text = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
cout << "After Reverse : [" << text << "]\n";
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
cout << "After Reverse : [" << text << "]\n";
return 0;
}
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
// Java program
// Reverse words in a given string
class MyString
{
//Reverse words in given text from end to beginning
public String reverse_words(String str)
{
String auxiliary = "";
String result = "";
//Loop contolling variables
int i = 0, j = 0;
System.out.print("\nGiven String : [" + str + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str.length(); i++)
{
if (str.charAt(i) != ' ')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str.length(); j++)
{
//Check that given character is space character or not
if (str.charAt(j) != ' ')
{
//When not get space
auxiliary = auxiliary + str.charAt(j);
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
}
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
String text = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
System.out.print("After Reverse : [" + text + "]\n");
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
System.out.print("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
// C# program
// Reverse words in a given string
//Include namespace system
using System;
class MyString
{
//Reverse words in given text from end to beginning
public String reverse_words(String str)
{
String auxiliary = "";
String result = "";
//Loop contolling variables
int i = 0, j = 0;
Console.Write("\nGiven String : [" + str + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str.Length; i++)
{
if (str[i] != ' ')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str.Length; j++)
{
//Check that given character is space character or not
if (str[j] != ' ')
{
//When not get space
auxiliary = auxiliary + str[j];
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
}
return result;
}
public static void Main(String[] args)
{
MyString obj = new MyString();
String text = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
Console.Write("After Reverse : [" + text + "]\n");
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
Console.Write("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
# Python 3 program
# Reverse words in a given string
class MyString :
# Reverse words in given text from end to beginning
def reverse_words(self, str) :
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("Before Reverse : [{0}]".format(str))
# Loop which is iterate given string character elements
while (i < len(str)) :
if (str[i] != ' ') :
# reset variable
auxiliary = ""
# loop, combine string text
j = i
while (j < len(str)) :
# Check that given character is space character or not
if (str[j] != ' ') :
# When not get space
auxiliary = auxiliary + str[j]
else :
# When find space break the loop and include space
auxiliary = " "+ auxiliary
break
j += 1
# Add the current get work into the result
result = auxiliary + result
i = j
else :
result = " "+ result
i += 1
return result
def main() :
obj = MyString()
text = "Are You Ready To Code"
# Check Case
text = obj.reverse_words(text)
print("After Reverse : [{0}]".format(text))
text = "problem interesting very is that"
# Check Case
text = obj.reverse_words(text)
print("After Reverse : [{0}]".format(text))
if __name__ == "__main__": main()
Output
Before Reverse : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Before Reverse : [problem interesting very is that]
After Reverse : [that is very interesting problem]
# Ruby program
# Reverse words in a given string
class MyString
# Reverse words in given text from end to beginning
def reverse_words(str)
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\nGiven String : [", str ,"]\n")
# Loop which is iterate given string character elements
while (i < str.length())
if (str[i] != ' ')
# reset variable
auxiliary = ""
# loop, combine string text
j = i
while (j < str.length())
# Check that given character is space character or not
if (str[j] != ' ')
# When not get space
auxiliary = auxiliary + str[j]
else
# When find space break the loop and include space
auxiliary = " "+ auxiliary
break;
end
j += 1
end
# Add the current get work into the result
result = auxiliary + result
i = j
else
result = " "+ result
end
i += 1
end
return result
end
end
def main()
obj = MyString.new()
text = "Are You Ready To Code"
# Check Case
text = obj.reverse_words(text)
print("After Reverse : [", text ,"]\n")
text = "problem interesting very is that"
# Check Case
text = obj.reverse_words(text)
print("After Reverse : [", text ,"]\n")
end
main()
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
import scala.util.control.Breaks._
// Scala program
// Reverse words in a given string
class MyString
{
//Reverse words in given text from end to beginning
def reverse_words(str: String): String = {
var auxiliary: String = "";
var result: String = "";
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\nGiven String : [" + str + "]\n");
//Loop which is iterate given string character elements
while (i < str.length())
{
if (str(i) != ' ')
{
//reset variable
auxiliary = "";
//loop, combine string text
j = i;
breakable {
while (j < str.length())
{
//Check that given character is space character or not
if (str(j) != ' ')
{
//When not get space
auxiliary = auxiliary + str(j);
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
j += 1;
}
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
i += 1;
}
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
print("After Reverse : [" + text + "]\n");
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
print("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
// Swift program
// Reverse words in a given string
class MyString
{
//Reverse words in given text from end to beginning
func reverse_words(_ text: String) -> String
{
let str = Array(text);
var auxiliary: String = "";
var result: String = "";
//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.count)
{
if (str[i] != " ")
{
//reset variable
auxiliary = "";
//loop, combine string text
j = i;
while (j < str.count)
{
//Check that given character is space character or not
if (str[j] != " ")
{
//When not get space
auxiliary = auxiliary + String(str[j]);
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
j += 1;
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
i += 1;
}
return result;
}
}
func main()
{
let obj: MyString = MyString();
var text: String = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
print("After Reverse : [\(text)] \n", terminator: "");
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
print("After Reverse : [\(text)]\n", terminator: "");
}
main();
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
<?php
// Php program
// Reverse words in a given string
class MyString
{
//Reverse words in given text from end to beginning
public function reverse_words($str)
{
$auxiliary = "";
$result = "";
//Loop contolling variables
$i = 0;
$j = 0;
echo "\nGiven String : [". $str ."]\n";
//Loop which is iterate given string character elements
for ($i = 0; $i < strlen($str); $i++)
{
if ($str[$i] != ' ')
{
//reset variable
$auxiliary = "";
//loop, combine string text
for ($j = $i; $j < strlen($str); $j++)
{
//Check that given character is space character or not
if ($str[$j] != ' ')
{
//When not get space
$auxiliary = $auxiliary . $str[$j];
}
else
{
//When find space break the loop and include space
$auxiliary = " ". $auxiliary;
break;
}
}
//Add the current get work into the result
$result = $auxiliary . $result;
$i = $j;
}
else
{
$result = " ". $result;
}
}
return $result;
}
}
function main()
{
$obj = new MyString();
$text = "Are You Ready To Code";
// Check Case
$text = $obj->reverse_words($text);
echo "After Reverse : [". $text ."]\n";
$text = "problem interesting very is that";
// Check Case
$text = $obj->reverse_words($text);
echo "After Reverse : [". $text ."]\n";
}
main();
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
// Node Js program
// Reverse words in a given string
class MyString
{
//Reverse words in given text from end to beginning
reverse_words(str)
{
var auxiliary = "";
var result = "";
//Loop contolling variables
var i = 0;
var j = 0;
process.stdout.write("\nGiven String : [" + str + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str.length; i++)
{
if (str[i] != ' ')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str.length; j++)
{
//Check that given character is space character or not
if (str[j] != ' ')
{
//When not get space
auxiliary = auxiliary + str[j];
}
else
{
//When find space break the loop and include space
auxiliary = " " + auxiliary;
break;
}
}
//Add the current get work into the result
result = auxiliary + result;
i = j;
}
else
{
result = " " + result;
}
}
return result;
}
}
function main()
{
var obj = new MyString();
var text = "Are You Ready To Code";
// Check Case
text = obj.reverse_words(text);
process.stdout.write("After Reverse : [" + text + "]\n");
text = "problem interesting very is that";
// Check Case
text = obj.reverse_words(text);
process.stdout.write("After Reverse : [" + text + "]\n");
}
main();
Output
Given String : [Are You Ready To Code]
After Reverse : [Code To Ready You Are]
Given String : [problem interesting very is that]
After Reverse : [that is very interesting problem]
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