String in CPP Assignment-1

Write C++ statements to do the following:

Q1

  1. Declare and initialize an array of scores of 20 batsmen of type int.
  1. Display the 10th component of the array scores.
  2. Display the 5th component of the array scores.
  3. Set the value of the 8th component of the array scores to 35.
  4. Set the value of the 6th component of the array scores to the sum of the 9th and 12th components of the array scores.
  5. Set the value of the 14th component of the array scores to 2 times the value of the 7th component minus 25. f. Display the array scores so that 4 components per line are printed

Q2.      Give the memory representation of the string str[]=”This is a fun”.

  1. What is the value of str[5] ?
  2. At what position is ‘\0’ ?
  3. What is the value of length?

Q3. Given the declaration:

char String[20];

State with reason whether the following statements are valid or invalid.

strcpy(String, “Programming is fun”);

  1. String = “I like C++”;
  2. cin >> String;
  3. if (String == “I like C ”) cout << String;
  4. String [6] = ‘t’;

Q2. Given the declaration:

char str1[15] = “Chirag Srivastava”;

char str2[15] = “Dhiman”;

State with reason whether the following statements are valid or invalid.

  1. str1 = str2;
  2. if (str1 == str2) cout << “strings are same.” ;
  3. if (strlen(str1) == strlen(str2)) cout << “strings are of the same length.” ;
  4. if (strcmp(str1, str2) < 0) cout << “str1 is less than str2.”;

Q3. Given the declaration:

char name[8] = “Newman”;

State with reason whether the following statements give the output as Newman

  1. cout<<name;
  2. for (int i=0;i;i < 5; i++) cout<<name[5];
  3. int j = 0; while (name[j] != ‘\0’) cout<<name[j++];

Q4.

  • Write a C++ statement that stores “GOOD MORNING” in str1.
  • Write a C++ statement that stores the length of str1 into int variable len.
  • Write a C++ statement that copies the value of name into str2. d) Write C++ code that outputs str1 if str1 is less than or equal to str2, and otherwise outputs str2.

 

Author: rakesh kumar

cbsetoday.com is my online home, here I share all the information related to education, programming - and everything else that attract me. You can get in touch with me in these social sharing websites

Leave a Reply