Reverse Repeat N Alphabets

The program must accept a string S and repeat the alphabets from the end till a new string S2 of length N is formed. Then the program must print the value of S2.

Boundary Condition(s):
1 <= Length of S <= 1000
1 <= N <= 10000

Input Format:
The first line contains S.
The second line contains N.

Output Format:
The first line contains S2.

Example Input/Output 1:
Input:
abc
5

Output:
cbacb

Explanation:
The alphabets are repeated from the end till the new string’s length is 5.

Example Input/Output 2:
Input:
work
10

Output:
krowkrowkr

#include<stdio.h>
#include <stdlib.h>

int main()
{
    char str1[100]={0},str2[100]={0};
    int i,j,k=0,n;
    scanf("%s",&str1);
    scanf("%d",&n);
    i=strlen(str1)-1;
    while(strlen(str2)<n)
    {
      if(i<0)
        {
            i=strlen(str1)-1;
        }
        str2[k]=str1[i];
        k++;
        i--;
    }
    printf("%s",str2);
}
Previous Article

Jooney's Amusement Park

Next Article

Reverse Column - First and Last

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *