Write a Program That Reads in Words and Prints Them Out in Reverse Order. Complete This Code.
Let there be a cord say "I AM A GEEK". And so, the output should be "GEEK A AM I" . This can done in many ways. One of the solutions is given in Reverse words in a string .
Examples:
Input : I AM A GEEK Output : GEEK A AM I Input : GfG IS THE BEST Output : BEST THE IS GfG
This can be done in more than simpler fashion by using the property of the "%due south format specifier" .
Belongings: %s will become all the values until it gets Nil i.e. '\0'.
Instance: char String[] = "I AM A GEEK" is stored equally shown in the paradigm beneath :
Approach: Traverse the string from the terminal character, and motility towards the start grapheme. While traversing, if a space character is encountered, put a Nil in that position and print the remaining cord just after the NULL grapheme. Repeat this until the loop is over and when the loop ends, print the string, the %s will brand the press of characters until it encounters the kickoff NULL graphic symbol.
Let us see the approach with the help of diagrams:
pace i: Traverse from the last graphic symbol until it encounters a space graphic symbol .
Pace 2: Put a Zip character at the position of space grapheme and impress the string afterward it.
Step iii: At the finish, the loop ends when it reaches the kickoff graphic symbol, and then print the remaining characters, information technology will be printed the outset Cipher character, hence the kickoff word will exist printed.
C++
#include <iostream>
using
namespace
std;
string wordReverse(string str)
{
int
i = str.length() - ane;
int
start, end = i + i;
string event =
""
;
while
(i >= 0) {
if
(str[i] ==
' '
) {
start = i + ane;
while
(start != end)
result += str[start++];
issue +=
' '
;
end = i;
}
i--;
}
start = 0;
while
(beginning != end)
result += str[showtime++];
return
result;
}
int
main()
{
string str =
"I AM A GEEK"
;
cout << wordReverse(str);
return
0;
}
C
#include <stdio.h>
#include <string.h>
void
printReverse(
char
str[])
{
int
length =
strlen
(str);
int
i;
for
(i = length - ane; i >= 0; i--) {
if
(str[i] ==
' '
) {
str[i] =
'\0'
;
printf
(
"%s "
, &(str[i]) + one);
}
}
printf
(
"%s"
, str);
}
int
chief()
{
char
str[] =
"I AM A GEEK"
;
printReverse(str);
return
0;
}
Java
import
java.io.*;
import
java.lang.*;
import
coffee.util.*;
course
GFG {
static
String wordReverse(String str)
{
int
i = str.length() -
ane
;
int
first, end = i +
1
;
String event =
""
;
while
(i >=
0
) {
if
(str.charAt(i) ==
' '
) {
offset = i +
1
;
while
(start != end)
result += str.charAt(start++);
result +=
' '
;
end = i;
}
i--;
}
start =
0
;
while
(showtime != end)
issue += str.charAt(start++);
return
result;
}
public
static
void
main(Cord[] args)
{
Cord str =
"I AM A GEEK"
;
System.out.print(wordReverse(str));
}
}
Python3
def
wordReverse(
str
):
i
=
len
(
str
)
-
1
start
=
end
=
i
+
1
outcome
=
''
while
i >
=
0
:
if
str
[i]
=
=
' '
:
start
=
i
+
i
while
start !
=
end:
result
+
=
str
[start]
commencement
+
=
1
result
+
=
' '
stop
=
i
i
-
=
ane
start
=
0
while
start !
=
end:
result
+
=
str
[start]
starting time
+
=
ane
return
result
str
=
'I AM A GEEK'
print
(wordReverse(
str
))
C#
using
Organization;
grade
GFG {
static
String wordReverse(String str)
{
int
i = str.Length - 1;
int
start, end = i + 1;
String consequence =
""
;
while
(i >= 0) {
if
(str[i] ==
' '
) {
start = i + 1;
while
(get-go != end)
effect += str[start++];
effect +=
' '
;
cease = i;
}
i--;
}
start = 0;
while
(start != end)
result += str[get-go++];
render
result;
}
public
static
void
Main()
{
String str =
"I AM A GEEK"
;
Panel.Write(wordReverse(str));
}
}
PHP
<?php
function
wordReverse(
$str
)
{
$i
=
strlen
(
$str
) - 1;
$end
=
$i
+ ane;
$result
=
""
;
while
(
$i
>= 0)
{
if
(
$str
[
$i
] ==
' '
)
{
$get-go
=
$i
+ 1;
while
(
$start
!=
$end
)
$result
=
$consequence
.
$str
[
$get-go
++];
$result
=
$outcome
.
' '
;
$end
=
$i
;
}
$i
--;
}
$start
= 0;
while
(
$starting time
!=
$finish
)
$result
=
$result
.
$str
[
$start
++];
return
$upshot
;
}
$str
=
"I AM A GEEK"
;
echo
wordReverse(
$str
);
?>
Javascript
<script>
function
wordReverse(str)
{
var
i = str.length - i;
var
offset, cease = i + 1;
var
result =
""
;
while
(i >= 0)
{
if
(str[i] ==
' '
)
{
first = i + 1;
while
(beginning != end)
result += str[start++];
upshot +=
' '
;
stop = i;
}
i--;
}
start = 0;
while
(start != end)
consequence += str[start++];
return
result;
}
var
str =
"I AM A GEEK"
;
document.write(wordReverse(str));
</script>
Output:
GEEK A AM I
Fourth dimension Complexity: O(len(str))
Auxiliary Infinite: O(len(str))
Without using any actress infinite:
Get through the string and mirror each word in the string, so, at the end, mirror the whole string.
The following C++ code can handle multiple contiguous spaces.
C++
#include <algorithm>
#include <iostream>
#include <string>
using
namespace
std;
string reverse_words(string s)
{
int
left = 0, i = 0, n = due south.size();
while
(s[i] ==
' '
)
i++;
left = i;
while
(i < n)
{
if
(i + one == north || s[i] ==
' '
)
{
int
j = i - 1;
if
(i + 1 == north)
j++;
while
(left < j)
swap(s[left++], south[j--]);
left = i + i;
}
if
(s[left] ==
' '
&& i > left)
left = i;
i++;
}
reverse(due south.begin(), southward.end());
return
s;
}
int
main()
{
cord str =
"I AM A GEEK"
;
str = reverse_words(str);
cout << str;
return
0;
}
Output:
GEEK A AM I
Time Complexity: O(len(str))
Auxiliary Space: O(one)
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, y'all tin also write an commodity using write.geeksforgeeks.org or mail your article to review-squad@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main folio and help other Geeks.
Delight write comments if you find annihilation wrong, or you want to share more than information about the topic discussed above.
Source: https://www.geeksforgeeks.org/print-words-string-reverse-order/
0 Response to "Write a Program That Reads in Words and Prints Them Out in Reverse Order. Complete This Code."
Post a Comment