Solve this problem in C++

Write a program that turns a new sentence upon taking an input string as per the below algorithm. Shift the alphabets n number of times in forwarding mode, where N is the length of a particular word in the given string.
Input: Hi Hell
Output: Jk Lipp
Explanation: H and I in the word HI shifted 2 times forward as the length of Hi is 2.
All the alphabets in the word “Hell:” are shifted 4 times forward as the length of hell is 4.
If the alphabet reaches the end of the series, then it should be replaced with z or Z depending on its case.
//KINDLY SOLVE IN C++

1 Like

import java.util.;
import java.lang.
;
import java.io.*;

public class wordShift {
public static String nextString(String temp, int wordLength){
String newWord=“”;

    for(int i=0;i<temp.length();i=i+1){
        char ch=temp.charAt(i);
        ch=(char)(ch+wordLength);
        newWord=newWord+(ch);
    }

    return newWord;
}
public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    String str=sc.nextLine();
    String temp="",ans="";
    int wordLength=0;

    for(int i=0;i<str.length();i=i+1){
        if(str.charAt(i)==' '){
            ans=ans+nextString(temp, wordLength);
            wordLength=0;
            temp="";
            ans=ans+" ";
        }
        else{
            temp=temp+str.charAt(i);
            wordLength=wordLength+1;
        }
    }

    ans=ans+nextString(temp, wordLength);
    System.out.println(ans);
}

}

My C++ code…
I hope it works.

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define endl “\n” // for fast input,output file
using namespace std;

int solve(string str,int start){
int len=0;
for(int i=start;i<str.length();i++){
if(str[i]==’ ') break;
else len++;
}
return len;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); // these 3 lines for fast I/P method
cout.tie(NULL);
int t;
cin>>t;
cin.ignore();
while(t–){
string str;
getline(cin,str);
int start=0;
int len=solve(str,start);
int index=start+len;
for(int i=0;i<str.length();i++){
if(str[i]!=’ '){
if(i<index){
str[i]=str[i]+(char)(len);
}
}

        else{
            len=solve(str,i+1);
            index=i+1+len;
        }
    }
    cout<<str<<endl;
}
return 0;

}

Here my code

1 Like

Thanks for the help.

Can you explain how "while (cin<<s) " Line 9
works

First of all
cin >> s
takes individual string that is if i provide
it “My name” in input it will take only “My”
as it considers Space as null character
and do not take any part after that
If was i used getline function which is used to take line as input the process might get complicated wo i havent used it here

So what
while ( cin >> s )
is doing
It is nothing the loop which is taking individual string as input and it will run until it encounters EOF as soon as EOF is encountered the loop will break .

EOF means end of file so
if you are providing input like "My Name
after “Name” there is EOF which will be encountered by while loop and it will terminate .

You can consider it as it will run until
Input is exhausted .
So if you provide “My name is chef”
then it will take “My” first and do operation on it after then “name” and so on .
As soon as it will finish work on “chef” it will search for next string input .But as there is nothing remaining in input it will simply terminate .

What is the ans= ans+nextString(temp, wordLength) outside for loop is doing?