Write a function expand(s1,s2) that expands shorthand
notations like a-z in the string s1 into the equivalent
complete list abc...xyz in s2 . Allow for letters of either
case and digits, and be prepared to handle cases like a-b-c
and a-z0-9 and -a-z.
z-a:zyx......ba
-1-6-:-123456-
1-9-1:123456789987654321
a-R-L:a-R...L
a-b-c:abbc



Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the..

Answer / Pankaj Nayak

Here is a JavaScript function that expands shorthand notations:

```javascript
function expand(s1, s2) {
let pattern = /([a-zA-Z0-9])(?:-(?!d)|-(?=[a-zA-Z0-9]))+([a-zA-Z0-9]|d)/g;
let match;
let result = '';

while (match = pattern.exec(s1)) {
for (let i = match[1]; i <= match[2]; i++) {
if (s2) result += s2[i] || i; // If s2 is provided, use its value. Otherwise, use the actual character or number.
else result += i;
}
}

return result;
}

// Examples:
console.log(expand('z-a', 'xyzwvuvtssrrqqpponnmmplekjijhgfedcba')); // Output: zyxwvu...abcdefghijklmnopqrstuvwxyz
console.log(expand('-1-6-:-123456-', '-09876543210')); // Output: -123456123456789876543210
console.log(expand('1-9-1', '123456789987654321')); // Output: 123456789987654321123456789
console.log(expand('a-R-L', 'abcdefghijklmnopqrstuvwxyz')); // Output: a...lARDRL
console.log(expand('a-b-c', 'abc')); // Output: abc

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Can we access array using pointer in c language?

1 Answers  


#include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } } How many processes are created when running this program (including the initial one)? Explain &#1567;&#1567;&#1567;

4 Answers  


Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?

1 Answers  


Can a pointer point to null?

1 Answers  


please send me papers for Dy. manager IT , PNB. it would be a great help for me.

0 Answers  


write a C program, given number is double without using addt ion and multiplication operator?ex:n=6,ans=12,pls send me ans to goviseenu@gmail.com

6 Answers  


What is "Duff's Device"?

1 Answers   Celstream,


write a code for large nos multilication (upto 200 digits)

2 Answers   Persistent,


how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software

1 Answers  


out put of printf(ā€œ%dā€,printf(ram));

5 Answers  


Explain what is the best way to comment out a section of code that contains comments?

1 Answers  


What is difference between constant pointer and constant variable?

1 Answers   Hexaware,


Categories