Given the strings s1 and s2 , not necessarily of the same length, create a new s
ID: 3631435 • Letter: G
Question
Given the strings s1 and s2 , not necessarily of the same length, create a new string consisting of alternating charcters of s1 and s2 (that is, the first character of s1 followed by the first character of s2 , followed by the second character of s1 , followed by the second character of s2 , and so on. Once the end of either string is reached, no additional characters are added. For example, if s1 contained "abc" and s2 contained "uvwxyz" , then the new string should contain "aubvcw" . Assign the new string to the variable s3 .Explanation / Answer
I am assuming that the strings are already defined before so the code goes as
-----------------------------------------
l1=strlen(s1);
l2=strlen(s2);
i=0;
j=0;
k=0;
while( i<= l1 || j<=l2)
{
if (i<=l1)
{
s3[k]=s1[i];
i=i+1;
k=k+1;
}
if (j<=l2)
{
s3[k]=s2[j];
j=i+1;
k=k+1;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.