Computer Science, asked by rajeshreddyb2002, 9 hours ago

Given a string and an integer K, print the Kth character after every character in the string.

For example, if the string is abc and k is 2 then the output is cde.

Input format:
First-line contains a string (0<=strlen<=100). Second-line contains an integer K.

Output format:
Print the Kth character after every character of the string.

Sample Input:
zldes

3

output:
coghv

Answers

Answered by Equestriadash
1

The following co‎des have been written using Python.

\tt s\ =\ in put("Enter\ a\ string:\ ")\\k\ =\ int(in put("Enter\ an\ integer:\ "))\\ns\ =\ ""\\for\ i\ in\ s:\\{\ \ \ \ \ }sn\ =\ k\\{\ \ \ \ \ }av\ =\ ord(i)\\{\ \ \ \ \ }if\ av\ +\ k\ &gt;\ 122:\\{\ \ \ \ \ }{\ \ \ \ \ }k\ =\ k\ -\ (122\ -\ av)\\{\ \ \ \ \ }{\ \ \ \ \ }k\ =\ k\%26\\{\ \ \ \ \ }{\ \ \ \ \ }ns\ =\ ns\ +\ ch r(96\ +\ k)\\{\ \ \ \ \ }else:\\{\ \ \ \ \ }{\ \ \ \ \ }ns\ =\ ns\ +\ ch r(av\ +\ k)\\    k\ =\ sn\\print(ns)

Once the string and the integer have been input, we create a new variable with an empty string to store the new and finalized string. To start the replacing process, we use a for loop, which is an iteration statement used to perform repeated checking. We give the range for the loop as the string, therefore, the variable traverses through each character in the string. We store the value of 'k' into a new variable since there is a possibility of k's value being changed during the process. After that, we take the ASCII value of the character and check if the character, when added to 'k', crosses 122 or not. If it does, we perform a shift to bring it back to the range 97 - 122. If it doesn't cross 122, we perform the addition as usual and print the new string.

Similar questions