Write a Program That Reads a Word and Prints the Word in Reverse.

Let at that place be a string say "I AM A GEEK". So, the output should be "GEEK A AM I" . This tin done in many means. One of the solutions is given in Opposite words in a string .

Examples:

Input : I AM A GEEK Output : GEEK A AM I  Input : GfG IS THE BEST Output : All-time THE IS GfG

This can be done in more simpler fashion by using the holding of the "%s format specifier" .
Property: %s volition become all the values until it gets Cypher i.e. '\0'.

Example: char String[] = "I AM A GEEK" is stored as shown in the image beneath :

Approach: Traverse the string from the last character, and motility towards the get-go character. While traversing, if a infinite character is encountered, put a Nix in that position and print the remaining string just after the NULL graphic symbol. Repeat this until the loop is over and when the loop ends, print the string, the %south volition make the printing of characters until it encounters the starting time NULL character.

Let the states see the approach with the assistance of diagrams:
pace i: Traverse from the last graphic symbol until it encounters a infinite character .

Step 2: Put a NULL grapheme at the position of space character and print the string subsequently it.

Step three: At the end, the loop ends when it reaches the first character, then print the remaining characters, it will be printed the get-go Nada grapheme, hence the showtime give-and-take volition exist printed.


C++

#include <iostream>

using namespace std;

string wordReverse(string str)

{

int i = str.length() - 1;

int start, end = i + i;

string outcome = "" ;

while (i >= 0) {

if (str[i] == ' ' ) {

first = i + 1;

while (start != finish)

consequence += str[first++];

result += ' ' ;

end = i;

}

i--;

}

commencement = 0;

while (starting time != end)

event += str[offset++];

return result;

}

int master()

{

string str = "I AM A GEEK" ;

cout << wordReverse(str);

return 0;

}

C

#include <stdio.h>

#include <cord.h>

void printReverse( char str[])

{

int length = strlen (str);

int i;

for (i = length - 1; i >= 0; i--) {

if (str[i] == ' ' ) {

str[i] = '\0' ;

printf ( "%s " , &(str[i]) + 1);

}

}

printf ( "%s" , str);

}

int main()

{

char str[] = "I AM A GEEK" ;

printReverse(str);

return 0;

}

Java

import java.io.*;

import java.lang.*;

import java.util.*;

class GFG {

static String wordReverse(String str)

{

int i = str.length() - 1 ;

int start, cease = i + ane ;

String result = "" ;

while (i >= 0 ) {

if (str.charAt(i) == ' ' ) {

start = i + 1 ;

while (start != end)

issue += str.charAt(first++);

result += ' ' ;

cease = i;

}

i--;

}

start = 0 ;

while (beginning != finish)

issue += str.charAt(get-go++);

return effect;

}

public static void primary(String[] args)

{

String str = "I AM A GEEK" ;

Organization.out.print(wordReverse(str));

}

}

Python3

def wordReverse( str ):

i = len ( str ) - 1

start = finish = i + 1

upshot = ''

while i > = 0 :

if str [i] = = ' ' :

start = i + 1

while starting time ! = end:

upshot + = str [start]

start + = 1

effect + = ' '

end = i

i - = 1

commencement = 0

while offset ! = end:

result + = str [first]

beginning + = one

render result

str = 'I AM A GEEK'

print (wordReverse( str ))

C#

using Organisation;

grade GFG {

static String wordReverse(Cord str)

{

int i = str.Length - 1;

int start, stop = i + i;

Cord event = "" ;

while (i >= 0) {

if (str[i] == ' ' ) {

start = i + 1;

while (first != end)

event += str[start++];

result += ' ' ;

end = i;

}

i--;

}

start = 0;

while (beginning != end)

effect += str[showtime++];

return result;

}

public static void Main()

{

String str = "I AM A GEEK" ;

Console.Write(wordReverse(str));

}

}

PHP

<?php

office wordReverse( $str )

{

$i = strlen ( $str ) - 1;

$end = $i + 1;

$result = "" ;

while ( $i >= 0)

{

if ( $str [ $i ] == ' ' )

{

$starting time = $i + one;

while ( $start != $cease )

$event = $result . $str [ $start ++];

$effect = $result . ' ' ;

$end = $i ;

}

$i --;

}

$kickoff = 0;

while ( $outset != $finish )

$result = $result . $str [ $starting time ++];

render $result ;

}

$str = "I AM A GEEK" ;

repeat wordReverse( $str );

?>

Javascript

<script>

function wordReverse(str)

{

var i = str.length - one;

var offset, end = i + 1;

var result = "" ;

while (i >= 0)

{

if (str[i] == ' ' )

{

start = i + 1;

while (commencement != terminate)

outcome += str[start++];

result += ' ' ;

end = i;

}

i--;

}

start = 0;

while (start != stop)

result += str[start++];

return consequence;

}

var str = "I AM A GEEK" ;

certificate.write(wordReverse(str));

</script>

Output:

GEEK A AM I

Fourth dimension Complexity: O(len(str))

Auxiliary Infinite: O(len(str))

Without using whatever extra space:

Get through the string and mirror each discussion in the string, then, at the end, mirror the whole string.

The following C++ code tin handle multiple contiguous spaces.

C++

#include <algorithm>

#include <iostream>

#include <string>

using namespace std;

string reverse_words(string south)

{

int left = 0, i = 0, n = s.size();

while (s[i] == ' ' )

i++;

left = i;

while (i < n)

{

if (i + 1 == northward || s[i] == ' ' )

{

int j = i - one;

if (i + 1 == northward)

j++;

while (left < j)

swap(s[left++], southward[j--]);

left = i + 1;

}

if (s[left] == ' ' && i > left)

left = i;

i++;

}

reverse(s.brainstorm(), s.cease());

return southward;

}

int main()

{

string str = "I AM A GEEK" ;

str = reverse_words(str);

cout << str;

return 0;

}

Output:

GEEK A AM I

Fourth dimension Complexity: O(len(str))

Auxiliary Infinite: O(1)

This article is contributed by MAZHAR IMAM KHAN. If you lot like GeeksforGeeks and would like to contribute, yous can also write an article using write.geeksforgeeks.org or mail service your article to review-squad@geeksforgeeks.org. See your article actualization on the GeeksforGeeks main folio and assistance other Geeks.
Delight write comments if you lot notice anything wrong, or you desire to share more information near the topic discussed above.


pereiraheyes1944.blogspot.com

Source: https://www.geeksforgeeks.org/print-words-string-reverse-order/

0 Response to "Write a Program That Reads a Word and Prints the Word in Reverse."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel