Reverse Last K Words
Reverse Last K Words
Problem Statement:
The problem must accept an integer K and a string S as the input. The program must reverse the last K words in the string S. Then the program must print the modified string S as the input.
Boundary Condition(s):
3 <= Length of S <= 100
1 <= K <=Number of words in S
Input Format:
The first line contain the value of K.
The second line contains the string S.
Output Format:
The first line contains the modified string S.
Example Input/Output 1:
Input:
3
There is nothing permanent except change
Output:
There is nothing change except permanent
Explanation:
The last 3 words "permanent except change" are reversed and the remaining words are printed as it is.
Example Input/Output 2:
Input:
9
There is no charm equal to tenderness of heart
Output:
heart of tenderness to equal charm no is There
Note:
Max Execution Time Limit: 4000 millisecs
Solution:
Programming Language: C Language
#include <stdio.h> #include <stdlib.h> int main(){ char a[500][1001]; int i=0,k; scanf("%d",&k); while(scanf("%s",a[i])==1){ i++; } int tw=i; for(int j=0;j<tw-k;j++){ printf("%s ",a[j]); } i=tw-1; while(k--){ printf("%s ",a[i--]); } }
//Published by PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments