Shift Characters Upwards

 Shift Characters Upwards 

PROBLEM STATEMENT :

The program must accept N string values of equal length L and an integer K as the input. The program must perform the following operation K times. - Shift the middle L-2 characters towards the top by one position among the given N string values. Finally, the program must print the N modified string values as the output. 

Boundary Condition(s):

2 <= N <= 100 

3 <= Length of each string <= 100 

1 <= K <= 10^8 

Input Format: 

The first line contains N. 

The next N lines, each contains a string value. 

The (N+2)nd line contains K. 

Output Format: 

The first N lines containing the N modified string values. 

Example Input/Output 1: 
Input: () 
5 
coding 
across 
expert 
office 
travel 
1  
Output: ccrosg axpers effict oravee todinl

Explanation: 

Here N = 5 and K = 1. 

The length of each string is 6. 

K = 1: After shifting the middle 4(6-2) characters towards the top by one position, the string values become 

ccrosg 

axpers 

effict 

oravee 

todinl 

Example Input/Output 2: 
Input: () 
5 
coding 
across 
expert 
office 
travel 
2  
Output: cxperg affics eravet oodine tcrosl




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int n,i,j,k,shift; scanf("%d",&n); char s[1001][1001],temp[1001]; for(i=0;i<n;++i) { scanf("%s",s[i]); } scanf("%d",&k); k=k%n; for(i=0;i<n;++i) { printf("%c",s[i][0]); shift=(i+k)%n; for(j=1;j<strlen(s[shift])-1;++j) { printf("%c",s[shift][j]); } printf("%c",s[i][strlen(s[i])-1]); printf("\n"); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int n,i,k; cin>>n; string s[n],res; for(i=0;i<n;++i) { cin>>s[i]; } cin>>k; for(i=0;i<n;++i) { string res=s[i][0]+s[(i+k)%n].substr(1,s[i].size()-2)+s[i][s[i].size()-1]; cout<<res<<"\n"; } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),i; String[] s=new String[n]; for(i=0;i<n;++i) { s[i]=sc.next().trim(); } int k=sc.nextInt(); for(i=0;i<n;++i) { String a=s[i].charAt(0)+""; String b=s[(i+k)%n].substring(1,s[(i+k)%n].length()-1); String c=s[i].charAt(s[i].length()-1)+""; String res=a+b+c; System.out.println(res); } } }



PYTHON

       

import copy n=int(input()) l=[list(input().strip()) for i in range(n)] res=copy.deepcopy(l) #For copying the list with seperate object k=int(input()) k=k%n; for i in range(n): res[i][1:-1]=l[(i+k)%n][1:-1] for i in res: print(*i,sep='')



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right