Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

REVERSE EACH WORD IN A GIVEN INPUT BY

RECURSION

#include<iostream>

#include<stack> // here we add a additional file called as stack

// The Standard Template Library (STL) in C++ contains the implementation of


 // commonly known data structures such as arrays, lists, stacks

using namespace std;


// huma syed make this function to reverse my sentence(input)

void reversesentence (string s){

stack<string> st; // we will insert words in this ok broo

for (int i=0; i< s.length(); i++){ //loop

string word=” “; // here we will store our words

while ( s[i]!= ’ ‘ && i< s.length ()){ /*check ky s[i]!= ‘ ‘ jb tk ho ak word hn or length bi
string sy

zyada na ho dyan rakhna hn */

word+=s[i];

i++;

st.push(word)

While(!st.empty(){})
int main(){

string s = “Hey ,how are you doing?


/*we would start traverse from 0 index of string and when ever we find a space we will came to know
our one word is complete*/

/*then we will pop each and every element then we will push it back into the sack to have the required
output  go ahead huma*/

Stack<int> st; //here I will mention the data type of stack

CHECK WHETER THIS ARRAY IS


SORTABLE:-
#include <iostream>

#include<stack>

using namespace std;

// Function to check if A[] is

// Stack Sortable or Not.

bool check(int A[], int N)

    // Stack S

    stack<int> S;

    // Pointer to the end value of array B.

    int B_end = 0;

    // Traversing each element of A[] from starting

    // Checking if there is a valid operation

    // that can be performed.

    for (int i = 0; i < N; i++)

    {
        // If the stack is not empty ask its not empty programming maam

        if (!S.empty())

        {

            // Top of the Stack  intialize

            int top = S.top();

            // If the top of the stack is

            // Equal to B_end+1, we will pop it

            // And increment B-end by 1.

            while (top == B_end + 1)

            {

                // if current top is equal to

                // B_end+1, we will increment

                // B_end to B_end+1

                B_end = B_end + 1;

                // Pop the top element.

                S.pop();
                // If the stack is empty We cannot

//Leave it alone khallli hn abbb

                // Therefore break

                if (S.empty())

                {

                    break;

                }

                // Current Top

                top = S.top();

            }

            // If stack is empty

            // Push the Current element

            if (S.empty()) {

                S.push(A[i]);

            }

            else

            {

                top = S.top();
                // If the Current element of the array A[]

                // if smaller than the top of the stack

                // We can push it in the Stack.

                if (A[i] < top)

                {

                    S.push(A[i]);

                }

                // Else We cannot sort the array

                // Using any valid operations.

                else

                {

                    // Not Stack Sortable shukar ALLAH PAK KA

                    return false;

                }

            }

        }

        else

        {

            // If the stack is empty push the current


            // element in the stack.

            S.push(A[i]);

        }

    }

    // Stack Sortable

    return true;

 // RUN IT NOWWWWWW

int main()

    int A[] = { 4, 1, 2, 3 };

    int N = sizeof(A) / sizeof(A[0]);

    check(A, N)? cout<<"YES": cout<<"NO";

    return 0;

Output:
YES

You might also like