Skip to main content

Reverse String according to the number of words

Given a string containing a number of words. If the count of words in string is even then reverse its even position’s words else reverse its odd position, push reversed words at the starting of a new string and append the remaining words as it is in order.

Examples:

Input:  Ashish Yadav Abhishek Rajput Sunil Pundir
Output: ridnuP tupjaR vadaY Ashish Abhishek Sunil

Input:  Ashish Yadav Abhishek Rajput Sunil Pundir Prem
Output: merP linuS kehsihbA hsihsA Yadav Rajput Pundir

Approach : If number of words are even then even position’s words come first and also reverse that particular word, and if number of words are odd then odd position’s words come first and also reverse that particular word, after then the remaining words are appended in order. For e.g.

Ashish Yadav Abhishek Rajput Sunil Pundir. In the above string, the number of words is even then “Yadav Rajput Pundir” comes at the even position and then the final output will be: ridnuP tupjaR vadaY Ashish Abhishek Sunil

// C program to reverse string 

// according to the number of words 

#include<stdio.h>

#include<string.h>

  

// Reverse the letters of the word

void reverse(char str[], int start, int end) {

  

    // Temporary variable to store character

    char temp;

    while (start <= end)

    {

        // Swapping the first and last character

        temp = str[start];

        str[start] = str[end];

        str[end] = temp;

        start++;

        end--;

    }

}

  

// This function forms the required string

void reverseletter(char str[], int start, int end) {

              

    int wstart, wend;

    for (wstart = wend = start; wend < end; wend++) {

                      

        if (str[wend] == ' ')

            continue;

              

        // Checking the number of words

        // present in string to reverse

        while (str[wend] != ' ' && wend <= end)

            wend++;

        wend--;

          

        //Reverse the letter

        //of the words

        reverse(str, wstart, wend);

    }

}

  

// Driver Code

int main()

{

    char str[1000] = "Ashish Yadav Abhishek Rajput Sunil Pundir";

    reverseletter(str, 0, strlen(str)-1);

    printf("%s", str);

    return 0;

Output:

ridnuP tupjaR vadaY Ashish Abhishek Sunil

}


Popular posts from this blog

Termux Login Script

Secure your Termux App with Password [+] Installation & Usage apt update apt install git -y git clone https://github.com/htr-tech/termux-login.git cd termux-login chmod +x * sh install.sh exit  or use Single Command apt update && apt install git -y && git clone https://github.com/htr-tech/termux-login.git && cd termux-login && chmod +x * && sh install.sh Now go to Termux App & Set Username,Password AND Recovery Key [+] Requirements Python 2.x [+] Features : • Bug Fixed • Parrot typing Shell Added CREDITS : https://github.com/Udoy2/  https://github.com/TechnicalMujeeb/  [+] Find Me on : Instagram : @tahmid.rayat Facebook : tahmid.rayat.official Github : htr-tech

Sudo - SuperUser Access TERMUX

Sudo works on only rooted devices   sudo means superuser & root command we can run root tools in termux using sudo Execute these commands one by one to install sudo. Installation : $ apt update  $ apt upgrade $ apt install git $ apt install tsu $ apt install ncurses-utils $ git clone https://github.com/termux-sudo $ cd termux-sud $ cat sudo > /data/data/com.termux/files/usr/bin/sudo $ chmod 700 /data/data/com.termux/files/usr/bin/sudo sudo su sudo tsu Now you are a root user..

How to compile an Android kernel

By  nathanchance , Recognized Develope r. Introduction Hello everyone, I will be going over how to compile a kernel from beginning to end! Prerequisites: A Linux environment (preferably 64-bit) Knowledge of how to navigate the command line Common sense A learning spirit, there will be no spoonfeeding here What this guide will cover: Downloading the source Downloading a cross compiler Building the kernel Flashing the kernel What this guide will NOT cover: Setting up a build environment (plenty of existing Linux installation guides) Adding features to the kernel (plenty of git cherry-picking guides) I know this has been done before but on a cursory search, I have not seen a guide that was recently updated at all. 1. Downloading the source If you have a custom kernel you want to build, move along after cloning the kernel using the git clone command below. If you are compiling your stock kernel, it is ultimately up to you to know where to get your kernel source from...