String in Cpp- Assignment No-1
Write C++ statements to do the following:
Q1
- Declare and initialize an array of scores of 20 batsmen of type int.
- Display the 10th component of the array scores.
- Display the 5th component of the array scores.
- Set the value of the 8th component of the array scores to 35.
- Set the value of the 6th component of the array scores to the sum of the 9th and 12th components of the array scores.
- 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”.
- What is the value of str[5] ?
- At what position is ‘\0’ ?
- 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”);
- String = “I like C++”;
- cin >> String;
- if (String == “I like C ”) cout << String;
- 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.
- str1 = str2;
- if (str1 == str2) cout << “strings are same.” ;
- if (strlen(str1) == strlen(str2)) cout << “strings are of the same length.” ;
- 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
- cout<<name;
- for (int i=0;i;i < 5; i++) cout<<name[5];
- 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.