Computer Science, asked by dalton6899, 7 months ago

Problem statement
You are required to implement the following function:
char* RepeatCharacter(char stri), char c, int n);
The function accepts a string 'str' of even length, character 'd' and an
integer 'n' as its argument. You are required to repeat the character
'c', 'n' number of times in the middle of even length string 'str', and
return the same.
Note:
If 'str is empty, return NULL, None in case of Python
n 20
Assumptions:
Length of string 'str' s 100000 characters​

Answers

Answered by dreamrob
12

Program in Python:

def RepeatCharacter(stri, c, n):

   length = len(stri)

   if (length % 2 != 0):

       print("The length of the string is not even")

   else:

       half = (int)(length / 2)

       new = stri[0 : half] + c * n + stri[half: length]

       return new

   

stri = input("Enter a string of even length : ")

c = input("Enter a character : ")

n = int(input("Enter a number : "))

if n > 20 or len(stri) > 100000:

   print("Invalid Input")

elif stri == "":

   print("None")

else:

   print("New string : ", RepeatCharacter(stri, c, n))

       

Output:

Enter a string of even length : abcd

Enter a character : M

Enter a number : 5

New string :  abMMMMMcd

Answered by bhagyashreenarwade93
0

Answer:

import java.util.*;

import java.io.*;

public class Prb

{

 public static void main (String[] args) {

       Scanner in = new Scanner(System.in);

       String inputString;

       inputString=in.nextLine();

       Character c=in.next().charAt(0);

       int count=in.nextInt();

       if(inputString.length()%2==0){

       System.out.println(processString(inputString,c,count));

       }

       else{

           System.out.println("invalid string");

       }

   }

 public static String processString(String s,char c,int count){

     int strlen = s.length();

      String one = "";

      String  two="";

     

     for(int i=0;i<s.length()/2;i++){

         one = one+s.charAt(i);

     }

     for(int j=s.length()/2;j<s.length();j++){

         two = two+s.charAt(j);

         

     }

     String part2="";

     for(int i=1;i<=count;i++)

         part2=part2+c;

         String pqr=one+part2+two;

         return(pqr);

 

   

}

     

   

}

 

   

Explanation:

Program in Java

Similar questions