Ask Question
2 July, 15:46

Mix 'em Let s1 and s2 be 2 c-strings of the same length. Write a function char * mixem (char*s1, char * s2) which returns a c-string made up of the characters of s1 and s2 "interleaved", starting with the first character of s1. Example: S1="abc", s2="123" The call mixem (s1, s2) returns the c-string "a1b2c3" Note: If s is a c-string, strlen (s) returns the length of the string (not counting the terminating null character '/0'.) Every c-string has a null character at the end.

+1
Answers (1)
  1. 2 July, 16:31
    0
    See Explaination

    Explanation:

    #include

    #include

    using namespace std;

    char * mixem (char * s1, char * s2);

    int main () {

    cout << mixem ("abc", "123") << endl;

    cout << mixem ("def", "456") << endl;

    return 0;

    }

    char * mixem (char * s1, char * s2) {

    char * result = new char[1 + strlen (s1) + strlen (s2) ];

    char * p1 = s1;

    char * p2 = s2;

    char * p = result;

    while (*p1 || * p2) {

    if (*p1) {

    *p = * p1;

    p1++;

    p++;

    }

    if (*p2) {

    *p = * p2;

    p2++;

    p++;

    }

    }

    *p = '/0';

    return result;

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Mix 'em Let s1 and s2 be 2 c-strings of the same length. Write a function char * mixem (char*s1, char * s2) which returns a c-string made ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers