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

1. In C Which of these is the correct way to set the value of a string variable?

ID: 3772556 • Letter: 1

Question

1. In C

Which of these is the correct way to set the value of a string variable?

(a) destination_string =
"This is the string contents that I want.";

(b) strcpy(destination_string,
"This is the string contents that I want.");

2. Suppose that a character string named my name has been declared and allocated at length 100. Write a statement that would set its contents to your name.

3. Suppose that a character string named my name has been declared and allocated at length 100. Write a printf statement that outputs to stdout the string variable’s contents, followed by a newline.

4. Suppose you have two character string variables declared, allocated and filled with contents (some text). How would you tell whether the first string were less than, equal to or greater than the second string?

Explanation / Answer

1.
(b) strcpy(destination_string,
"This is the string contents that I want.");

2.
strcpy(my_name, "Rajesh");

3.
printf("%s ",my_name);

4.
a=strcmp(s1,s2);
if(a==0)
   printf("Strings are equal");
if(a<0)
   printf("First String less than second");
if(a>0)
   printf("First String is greater than second");