Your keyboard is worn out after playing games for a long time. Now when you press the keys of the keyboard, the first character you press is pressed once, then the key you press is pressed twice, then the third character will be pressed once again, and the fourth character will be pressed twice and so on. Implement it using function.

 Problem Statement:

Your keyboard is worn out after playing games for a long time. Now when you press the keys of the keyboard, the first character you press is pressed once, then the key you press is pressed twice, then the third character will be pressed once again, and the fourth character will be pressed twice and so on. Implement it using function.


See the sample input-output for more clarification.


Sample Input-

-----------------------

abcd


Sample Output-

-----------------------

abbcdd




answer;



Here is a function in C that takes a string as input and returns a modified string according to the given condition:

char *modify_string(const char *str) { // Find the length of the input string int len = 0; for (const char *p = str; *p; p++) { len++; } // Allocate a buffer for the modified string char *modified = malloc((2 * len + 1) * sizeof(char)); // Modify the string char *p = modified; for (int i = 0; i < len; i++) { *p++ = str[i]; if ((i + 1) % 2 == 0) { *p++ = str[i]; } } *p = '\0'; return modified; }

This function uses a loop to iterate over the characters of the input string, and for each character it writes the character to the output string and then, if the index of the character is even, it writes the character again to the output string.

For example, if you call modify_string("abcd"), the function will return a pointer to the string "abbcdd".

Note that this function dynamically allocates memory for the modified string using malloc, so you need to call free to release the memory when you are done with the string.

Here is an example of how you can use this function:

#include <stdio.h> #include <stdlib.h> char *modify_string(const char *str); int main(void) { //

Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();