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
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
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
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()
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()
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
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";
Which of the following string functions returns the length of a string? a) strlen() b) strcpy() c) strcat() d) strcmp() Answer: a) strlen()
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
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
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
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.
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:
- 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:
cint strlen(char *string);
- 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:
arduinochar* strcpy(char *destination, const char *source);
The destination is the array where the source string will be copied.
- 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:
arduinochar* strcat(char *destination, const char *source);
- 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:
arduinoint strcmp(const char *string1, const char *string2);
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.
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.
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.
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.
char array[] = "string";
char* strcpy(char *destination, const char *source);
char* strcat(char *destination, const char *source);
int strcmp(const char *string1, const char *string2);