Rotate each words in a string
Here given code implementation process.
// Java program
// Rotate each words in a string
class MyString
{
//Rotate words in given text from front to end
public String rotate_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 = str.charAt(j) + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
}
//Add the current get work into the result
result = result + auxiliary;
i = j;
}
else
{
result = result + " ";
}
}
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
String text = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
System.out.print("After Reverse : [" + text + "]\n");
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
System.out.print("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
// C++ program
// Rotate each words in a string
//Include header file
#include <iostream>
using namespace std;
class MyString
{
public:
//Rotate words in given text from front to end
string rotate_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 = str[j] + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
}
//Add the current get work into the result
result = result + auxiliary;
i = j;
}
else
{
result = result + " ";
}
}
return result;
}
};
int main()
{
MyString obj = MyString();
string text = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
cout << "After Reverse : [" << text << "]\n";
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
cout << "After Reverse : [" << text << "]\n";
return 0;
}
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
// C# program
// Rotate each words in a string
//Include namespace system
using System;
class MyString
{
//Rotate words in given text from front to end
public String rotate_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 = str[j] + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
}
//Add the current get work into the result
result = result + auxiliary;
i = j;
}
else
{
result = result + " ";
}
}
return result;
}
public static void Main(String[] args)
{
MyString obj = new MyString();
String text = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
Console.Write("After Reverse : [" + text + "]\n");
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
Console.Write("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
<?php
// Php program
// Rotate each words in a string
class MyString
{
//Rotate words in given text from front to end
public function rotate_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 = $str[$j] . $auxiliary;
}
else
{
//When find space break the loop and include space
$auxiliary = $auxiliary ." ";
break;
}
}
//Add the current get work into the result
$result = $result . $auxiliary;
$i = $j;
}
else
{
$result = $result ." ";
}
}
return $result;
}
}
function main()
{
$obj = new MyString();
$text = "This is difficult algorithm";
// Check Case
$text = $obj->rotate_words($text);
echo "After Reverse : [". $text ."]\n";
//Modify text for new case
$text = "uoY wonk em i ma remmargorp";
// Check Case
$text = $obj->rotate_words($text);
echo "After Reverse : [". $text ."]\n";
}
main();
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
// Node Js program
// Rotate each words in a string
class MyString
{
//Rotate words in given text from front to end
rotate_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 = str[j] + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
}
//Add the current get work into the result
result = result + auxiliary;
i = j;
}
else
{
result = result + " ";
}
}
return result;
}
}
function main()
{
var obj = new MyString();
var text = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
process.stdout.write("After Reverse : [" + text + "]\n");
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
process.stdout.write("After Reverse : [" + text + "]\n");
}
main();
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
# Python 3 program
# Rotate each words in a string
class MyString :
# Rotate words in given text from front to end
def rotate_words(self, str) :
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\nGiven String : [{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 = str[j] + auxiliary
else :
# When find space break the loop and include space
auxiliary = auxiliary + " "
break
j += 1
# Add the current get work into the result
result = result + auxiliary
i = j
else :
result = result + " "
i += 1
return result
def main() :
obj = MyString()
text = "This is difficult algorithm"
# Check Case
text = obj.rotate_words(text)
print("After Reverse : [{0}]".format(text))
# Modify text for new case
text = "uoY wonk em i ma remmargorp"
# Check Case
text = obj.rotate_words(text)
print("After Reverse : [{0}]".format(text))
if __name__ == "__main__": main()
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
# Ruby program
# Rotate each words in a string
class MyString
# Rotate words in given text from front to end
def rotate_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 = str[j] + auxiliary
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 = result + auxiliary
i = j
else
result = result +" "
end
i += 1
end
return result
end
end
def main()
obj = MyString.new()
text = "This is difficult algorithm"
# Check Case
text = obj.rotate_words(text)
print("After Reverse : [", text ,"]\n")
# Modify text for new case
text = "uoY wonk em i ma remmargorp"
# Check Case
text = obj.rotate_words(text)
print("After Reverse : [", text ,"]\n")
end
main()
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
import scala.util.control.Breaks._
// Scala program
// Rotate each words in a string
class MyString
{
//Rotate words in given text from front to end
def rotate_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 = "" + str(j) + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
j += 1;
}
}
//Add the current get work into the result
result = result + auxiliary;
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 = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
print("After Reverse : [" + text + "]\n");
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
print("After Reverse : [" + text + "]\n");
}
}
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
// Swift program
// Rotate each words in a string
class MyString
{
//Rotate words in given text from front to end
func rotate_words(_ text: String) -> String
{
var str = Array(text);
var auxiliary: String = "";
var result: String = "";
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\nGiven String : [\(text)]");
//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 = String(str[j]) + auxiliary;
}
else
{
//When find space break the loop and include space
auxiliary = auxiliary + " ";
break;
}
j += 1;
}
//Add the current get work into the result
result = result + auxiliary;
i = j;
}
else
{
result = result + " ";
}
i += 1;
}
return result;
}
}
func main()
{
let obj: MyString = MyString();
var text: String = "This is difficult algorithm";
// Check Case
text = obj.rotate_words(text);
print("After Reverse : [\(text)]");
//Modify text for new case
text = "uoY wonk em i ma remmargorp";
// Check Case
text = obj.rotate_words(text);
print("After Reverse : [\(text)]");
}
main();
Output
Given String : [This is difficult algorithm]
After Reverse : [sihT si tluciffid mhtirogla]
Given String : [uoY wonk em i ma remmargorp]
After Reverse : [You know me i am programmer]
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