0

Program to remove "dub" from a string

The new type of music mix made with the word "dub" . The process is like this . Every orginal song must start with the word "dub" and after every word of the orginal lyric the word "dub" should be added. Suppose: Input: 37 dubneverdubgonnadubgivedubyoudubupdub Expect output : never gonna give you up How can i solve this program in c? Anyone please help me.

14th Dec 2020, 10:35 AM
Marop Hossain
Marop Hossain - avatar
3 Answers
+ 1
a slightly better JS approach might be console.log('dubneverdubgonnadubgivedubyoudubupdub'.replace(/dub/g, ' ')); wonder if C has replace() like others!?! a verbose C approach could be #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stddef.h> int main() { const char* s = "dubneverdubgonnadubgivedubyoudubupdub"; const char* pS = s; size_t sLen = strlen(s); char* buf = malloc(sizeof(char) * sLen); if (buf == NULL) { return 1; } memset(buf, 0, sLen); char* bP = buf; const char* pT = NULL; size_t pos = 0; while ((pS = strstr(pS, "dub")) != NULL) { pT = strstr(pS + 3, "dub"); if (pT == NULL) { break; } else { pos = pT - (pS + 3); strncpy(bP, pS + 3, pos); strcat(bP, " "); pS += 3; bP += (pos + 1); } } if ((pT = strrchr(buf, ' ')) != NULL) { buf[pT - buf] = '\0'; } printf("%s\n", buf); free(buf); return 0; } https://code.sololearn.com/cA24A62a24A7
14th Dec 2020, 12:28 PM
Flash
+ 6
For JavaScript "dubneverdubgonnadubgivedubyoudubupdub".split("dub").join(" ").trim("") https://code.sololearn.com/cvkzrp2oALbA/?ref=app
14th Dec 2020, 10:42 AM
Gordon
Gordon - avatar
0
Please use C language, not JavaScript or other languages.
14th Dec 2020, 11:14 AM
Marop Hossain
Marop Hossain - avatar