Reverse words in a given string

 Problem statement:- Program to Reverse words in a given string.

Example:-

              Input: Given

                                 String=C STRING PROGRAM

             Output: PROGRAM STRING C

            Input: Given

                                String=c++ string question

             Output: question string c++

Data requirement:-


   Input Data:- str

  Output Data:-
sub_str

  Additional Data:- 
i, p, in, arr, out

Program in C

Here is the source code of the C Program to Reverse words in a given string.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    int in=0,out=0,p=0;
    char str[100]={0},sub_str[100][100]={0};
    printf("Enter your String:");
    gets(str);

    //splitting Input String into sub string
    while(str[p]!='\0')
    {
        out=0;
        while(str[p]!=' ' && str[p]!='\0')
        {
            sub_str[in][out]=str[p];
            p++;
            out++;
        }
        sub_str[in][out]='\0';
        in++;
        if(str[p]!='\0')
        {
            p++;
        }
    }
    printf("After reversing words in a given string is:");
     //Here 'in' variable count number of word present in the given string
    for(out=in-1;out>=0;out--){
    printf("%s ",sub_str[out]);
        }
}

Input/Output:
Enter your String:C STRING PROGRAM
After reversing words in a given string is:PROGRAM STRING C

Program in C++

Here is the source code of the C++ Program to Reverse words in a given string.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int in=0,out=0,p=0;
    int len=0;
    char sub_str[100][100]={0};
    for(in=0;str[in]!='\0';in++)
    {
        len++;
    }
    //splitting Input String into sub string
    while(str[p]!='\0')
    {
        out=0;
        while(str[p]!=' ' && str[p]!='\0')
        {
            sub_str[in][out]=str[p];
            p++;
            out++;
        }
        sub_str[in][out]='\0';
        in++;
        if(str[p]!='\0')
        {
            p++;
        }
    }
    cout<<"After reversing words in a given string is :";
     //Here 'in' variable count number of word present in the given string
    for(out=in-1;out>=0;out--){
   cout<<sub_str[out]<<" ";
        }
}

Input/Output:
Enter your String:c++ string question
After reversing words in a given string is :question string c++

Program in Java

Here is the source code of the Java Program to Reverse words in a given string.

Code:

import java.util.Scanner;
public class ReversingWords {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
//splitting Input String into sub string
String[] sub_str=str.split("\\s");
int out;
System.out.print("After reversing words in a given string is:");
     //Here 'in' variable count number of word present in the given string
    for(out=sub_str.length-1;out>=0;out--){
    System.out.print(sub_str[out]+" ");
        }
    cs.close();
}
}

Input/Output:
Enter your String:
reversing words in a given string
After reversing words in a given string is:string given a in words reversing 

Program in Python

Here is the source code of the Python Program to Reverse words in a given string.

Code:

str=input("Enter Your String:")
sub_str=str.split(" ")
print("After reversing words in a given string is:")
for out in range(len(sub_str)-1,-1,-1):
    print(sub_str[out],end=" ")

Input/Output:
Enter Your String:python string question
After reversing words in a given string is:
question string python 

Post a Comment

0 Comments