12 Lecture

CS201

Midterm & Final Term Short Notes

Character Arrays

Character arrays in C/C++ programming are used to store strings of characters, such as words or sentences. They are declared as arrays of characters and can be initialized with a string of characters. Character arrays can be manipulated using va


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the size of a character array that can store a string of length 10? a) 10 b) 11 c) 12 d) 13 Answer: b) 11

  2. Which of the following is the correct way to initialize a character array? a) char array[] = "Hello"; b) char array[5] = "Hello"; c) char array[6] = {'H', 'e', 'l', 'l', 'o'}; d) All of the above Answer: d) All of the above

  3. Which of the following string functions can be used to concatenate two character arrays? a) strlen() b) strcpy() c) strcat() d) strcmp() Answer: c) strcat()

  4. Which of the following string functions can be used to copy one character array to another? a) strlen() b) strcpy() c) strcat() d) strcmp() Answer: b) strcpy()

  5. What is the output of the following code snippet? char str1[] = "Hello"; char str2[] = "World"; strcat(str1, str2); printf("%s", str1); a) Hello World b) World Hello c) Hello d) World Answer: a) Hello World

  6. Which of the following is not a valid way to declare a character array? a) char array[10]; b) char* array = "Hello"; c) char array[] = "Hello"; d) All of the above are valid Answer: b) char* array = "Hello";

  7. Which of the following string functions returns the length of a string? a) strlen() b) strcpy() c) strcat() d) strcmp() Answer: a) strlen()

  8. What is the output of the following code snippet? char str1[10] = "Hello"; char str2[10]; strcpy(str2, str1); printf("%s", str2); a) Hello b) World c) None of the above d) It will result in a compile-time error Answer: a) Hello

  9. What is the size of a character array that can store an empty string? a) 0 b) 1 c) 2 d) It is not possible to declare an empty character array Answer: b) 1

  10. Which of the following string functions returns a negative, zero, or positive value depending on whether the first string is less than, equal to, or greater than the second string? a) strlen() b) strcpy() c) strcat() d) strcmp() Answer: d) strcmp()



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a character array in C/C++ programming, and how is it different from a string? Answer: A character array is a collection of characters stored in contiguous memory locations. It is used to store strings in C/C++ programming. A string, on the other hand, is a collection of characters terminated by a null character. It is implemented using a character array and provides many built-in functions to manipulate the string.

  2. How can you initialize a character array with a string literal? Answer: A character array can be initialized with a string literal using the following syntax:


  1. What is the purpose of the strlen() function, and how is it used? Answer: The strlen() function is used to determine the length of a string, i.e., the number of characters in the string. It takes a string as input and returns an integer value. The syntax of the strlen() function is as follows:
c
int strlen(char *string);
  1. How can you copy one character array to another? Answer: One character array can be copied to another using the strcpy() function. The syntax of the strcpy() function is as follows:
arduino
char* strcpy(char *destination, const char *source);

The destination is the array where the source string will be copied.

  1. What is the purpose of the strcat() function, and how is it used? Answer: The strcat() function is used to concatenate two strings, i.e., to join two strings together to form a single string. It takes two strings as input and returns a pointer to the resulting concatenated string. The syntax of the strcat() function is as follows:
arduino
char* strcat(char *destination, const char *source);
  1. How can you compare two strings in C/C++ programming? Answer: Two strings can be compared using the strcmp() function. The strcmp() function returns a negative value if the first string is less than the second string, zero if the two strings are equal, and a positive value if the first string is greater than the second string. The syntax of the strcmp() function is as follows:
arduino
int strcmp(const char *string1, const char *string2);
  1. How can you convert a string to uppercase or lowercase in C/C++ programming? Answer: A string can be converted to uppercase or lowercase using the toupper() and tolower() functions, respectively. The toupper() function converts a lowercase character to uppercase, while the tolower() function converts an uppercase character to lowercase. These functions take a single character as input and return the converted character.

  2. What is a null character, and how is it used in strings? Answer: A null character, represented as '\0', is a special character used to terminate a string. It is used to mark the end of a string and is automatically added to the end of a string literal in C/C++ programming.

  3. How can you read a string from the console using the standard input stream in C/C++ programming? Answer: A string can be read from the console using the standard input stream, cin, in C++ programming. The getline() function can be used to read a line of input, including whitespace characters, and store it in a string variable.

  4. What is the maximum size of a character array that can be declared in C/C++ programming? Answer: The maximum size of a character array that can be declared in C/C++ programming is limited by the amount of available memory in the system. However, some compilers may impose a maximum size limit on character arrays.

Character arrays are used in C/C++ programming to store a sequence of characters. They are declared like any other array, with the data type 'char' and the number of elements specified within square brackets. Character arrays are often used to store strings, which are sequences of characters terminated by a null character ('\0'). To initialize a character array with a string literal, the syntax is as follows:
c
char array[] = "string";
The strlen() function can be used to determine the length of a string. It takes a string as input and returns the number of characters in the string, excluding the null character. To copy one character array to another, the strcpy() function can be used. The syntax is as follows:
arduino
char* strcpy(char *destination, const char *source);
The destination is the array where the source string will be copied. To concatenate two strings, the strcat() function can be used. The syntax is as follows:
arduino
char* strcat(char *destination, const char *source);
To compare two strings, the strcmp() function can be used. The function returns a negative value if the first string is less than the second string, zero if the two strings are equal, and a positive value if the first string is greater than the second string. The syntax is as follows:
arduino
int strcmp(const char *string1, const char *string2);
A string can be converted to uppercase or lowercase using the toupper() and tolower() functions, respectively. These functions take a single character as input and return the converted character. The maximum size of a character array that can be declared in C/C++ programming is limited by the amount of available memory in the system. However, some compilers may impose a maximum size limit on character arrays.