Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Suppose that str1, str2, and str3 are string variables. i. Write an C stateme

ID: 3680987 • Letter: 1

Question

1) Suppose that str1, str2, and str3 are string variables.

i. Write an C statement that concatenates str1 and str2, storing the result into str3. (str1 and str2 should not be changed.)

ii.Repeat part (i), using C++ instead of C. Assume that str1, str2, and str3 are string objects.

iii.Repeat part (i), using Ada instead of C.

2).  (i) If a is a two-dimensional C++ array, is it legal to use the expression a[i, j] in a program? If so, what is the meaning of a[i, j]?

(ii) If a is a two-dimensional Java array, is it legal to use the expression a[i, j] in a program? If so, what is the meaning of a[i, j]?

Explanation / Answer

1)

Suppose that str1, str2, str3 are string variables.

i)

In C, string concatenation can be done using strcat(). But it modifies one of the strings. So, asprintf() can be used to store the concatenated string.

asprintf(&str3, "%s%s", str1, str2);

Complete code is as follows:

#define _GNU_SOURCE

#include <stdio.h>

int main()

{

    char *str3;

    char *str1="Hai",*str2="Dioy";

    asprintf(&str3, "%s%s", str1, str2);

    printf("%s ",str3);

    return 0;

}

ii)

String concatenation in CPP can be done as follows:

str3=str1+str2;

Complete code is as follows:

#include <iostream>

using namespace std;

int main()

{

   string str1="hai",str2="dioyw",str3;

   str3=str1+str2;

   cout<<str3;

   return 0;

}

iii)

String concatenation in Ada can be done as follows:

Str3 : String := str1 & str2;

Complete code is as follows:

with Ada.Text_IO; use Ada.Text_IO;

procedure String_Concatenation is

   str1 : String := "Hello";

   str2 : String := "man";

begin

   declare

      str3 : String := str1 & str2;

   begin

      Put_Line (str3);

   end;

end String_Concatenation;

2)

(i)

In C++, a two-dimensional array cannot be used as a[i,j].

(ii)

In JAVA, a two-dimensional array cannot be used as a[i,j]. In Some other languages like C#, array can be used as a[i,j].