Strings and Character Arrays in C Tutorial And MCQ
Table of Contents
Learn Arrays in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
String is just like an array of elements of character (char) data type. Every valid string in c programming language must end with a NULL character or ‘\0’. ASCII value of a NULL character is 48. Without null character at the end, array is just an array of characters but not STRING.
Arrays in C
Strings and Character Arrays in C
C language provide ‘char’ data type to hold Alphabets and Special characters. Size of a single character is 1 Byte.
Let us examine a character array and see how to turn it in to a string. ‘string’ is not a keyword.
char names[] = {'a','b','c','d'};
we shall turn this character array to string by adding a NULL character at the end. NULL character is also called SLASH ZERO. You can use DOUBLE QUOTES to create a STRING.
char names[] = {'a','b','c','d', '\0'}; //SLASH ZERO (or) char names[] = "abcd";
What is the need to add SLASH ZERO at the end ?
You’ll use the String Library Functions in practise to find out a string’s length, copy strings, concatenate strings, and more. All of these functions seek to determine the character string’s final position in memory. These string functions are informed about the end of a string by the NULL character.
Note: SIZEOF() function always gives the Size of a STRING including ‘\0’. STELEN() function gives the Length of a string counting number of characters. So the size of a string is one more than the length of that string.
Arrays in C
Example 1: Printing a String with %C
int main() { char name[] = {'j','e','r','r','y','\0'}; int i=0; while(name[i] != '\0') { printf("%c", name[i]); i++; } return 9; }
We check each character against a NULL character. It it is not NULL we print it using “%c” format specifier.
C Programming examples
Example 2: Printing a String with %S
int main() { char name[] = {'j','e','r','r','y','\0'}; printf("%s", name); return 9; }
“%S” format specifier prints the whole string in one go. It will not print NULL character.
Strings Char Arrays
Example 3: Reading a String with %S
int main() { char name[10]; printf("ENTER a name..\n"); scanf("%s", name); printf("Entered=%s", name); return 9; } //INPUT > Enter a name.. //TOMCAT //OUTPUT: TOMCAT
Example 4: Reading a String with Spaces with GETS() function & PUTS() to print
You can not read a string with spaces using %S format specifier. So we use predefined Library function GETS() to read a Line of Text With Spaces.
#include<string.h> int main() { char name[10]; printf("ENTER a name..\n"); gets(name); puts(name); return 9; } //INPUT: > ENTER a name.. //TOM CAT //Entered=TOM CAT
C String Assignments
Just like assigning the value of one variable to another variable, you can not assign one String Variable to another.
Why do you want to assign one variable to another ?
Answer is that you want to copy the value of 1st variable to the second variable.
So use STRCPY() string library function to copy string value and assign to the second variable.
#include<string.h> int main() { char str1[] = "Hello"; char str2[10]; char str3[10]; str2 = str1; //Compiler error; strcpy(str3, str1); //works return 9; }
You can not change the value of whole string to another string constant. You can only change individual characters of a string. If you use a char pointer, you can change the string pointed by it. Let us try changing the value of a string variable in below example.
int main() { char str1[] = "Hello"; str1[1] = 'a'; // You can change individual characters str1 = "AFRICA"; //Error. You can not change char *p; p = "JIMMY"; p = "TOMMY"; //works. You are using pointers. return 8; }
String Arrays or Multidimensional Arrays in C
C language allows us to create Array of Strings or String Multidimensional Arrays. What developers use are just 2D String arrays that hold a number of 1D Char Array Strings.
Example 1: String Array – Fixed Length Strings
If you use plain String array without pointers, you can not change the maximum length of strings inside Array of Strings.
int main() { char names[5][10] = { "ARICA", "JAPAN", "SEA", "EGGS", "RIVERS" }; printf("%s", names[0]); printf(" %s, names[1]); names[0] = "PENCIL"; //ERROR. CAN NOT CHANGE. return 9; }
Example 2: String Array – Variable Length Strings
You change maximum length of Strings if you use CHARACTER POINTER to hold address of STRINGS. These STRINGS can be stored anywhere in memory. There is no need to allocate contiguous memory locations to store strings or char arrays in this pointer concept. Also you can reinitialize strings with other strings.
int main() { char *names[5] = { "ARICA", "JAPAN", "SEA", "EGGS", "RIVERS" }; printf("%s", names[0]); name[0] = "PEN AFRICA"; // CAN CHANGE printf(" %s, names[0]); return 9; }
Note: You can not use SCANF() function to scan strings using a CHAR POINTER like *name[0].
C String Library Functions Overview
C strings help us do some most common string operations like COPY, CONCATENATION, UPPERCASE, LOWERCASE and more. Here is a list of STRING function we most use.
SNO | Functions | Description |
---|---|---|
1 | strlen(source) | Returns length of a string without counting NULL character |
2 | strcpy(target, source) | Copies contents of one string to the other. Return type is void. |
3 | strcat(target, source) | Appends new string to the end of SOURCE string. Return type is void. |
4 | strcmp(string1, string2) | Compares string1 with string 2 and returns a number. >0 if greater, 0 for equal and <0 if string1 is less than string2. |
5 | strrevv(string) | Reverses a string and returns. |
6 | strupr(string) | Converts a string to uppercase and returns |
7 | strlwr(string) | Converts a string to lowercase and returns |
8 | toupr(char) | Converts a character to uppercase and returns |
9 | tolwr(char) | Converts a character to lowercase and returns |
10 | strchr(string, char1) | Finds first occurrence of a character in the given string |
11 | strstr(string, str1) | Finds first occurrence of a string str1 in a given string. |
12 | strrchr(string, char1) | Finds last occurrence of a character in a given string. |
13 | strrstr(string, str1) | Finds last occurrence of a string str1 , in a given string. |
[WpProQuiz 38]
Arrays in C Important Questions
1) What is the ASCII value of NULL or \0.?
A) 0
B) 1
C) 10
D) 49
Answer [=] A
Explanation:
ASCII value of NULL character is ZERO 0.
2) A character constant is enclosed by.?
A) Left Single Quotes
B) Right Single Quotes
C) Double Quotes
D) None of the above
Answer [=] B
Explanation:
char ary[] = {‘a’,’b’,’\0′}.
char bry[] = {`a`,`b`,`c`}; is wrong as it uses Left Single Quotes.
3) Choose a correct statement about C String.
A) A string is a group of characters enclosed by double quotes.
B) If a string is defined with double quotes, NULL is automatically added at the end.
C) Size of a string is without counting NULL character at the end
D) All the above
Answer [=] D
4) A C string elements are always stored in.?
A) Random memory locations
B) Alternate memory locations
C) Sequential memory locations
D) None of the above
Answer [=] C
5) What is the output of C program with strings.?
int main() { char var='b'; printf("%d ", sizeof("a")); printf("%d ", sizeof('b')); printf("%d ", sizeof(10)); printf("%d ", sizeof(var)); } //int size is 2 bytes
A) 1 1 1 1
B) 2 1 2 1
C) 2 2 2 1
D) 2 2 2 2
Answer [=] C
Explanation:
sizeof(‘b’) is 2 because ‘b’ is converted to ASCII number which is integer. sizeof(var) is printed as expected. size(“a”) is two because NULL occupies 1 byte.
6) What is the output of C program with strings.?
int main() { char str[]="JACKIE CHAN"; int i=0; while(str[i] != 0) { printf("%c",str[i]); i++; } return 0; }
A) JJJJJJ JJJJ
B) JACKIE CHAN
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Yes. You can check for end of a string with ASCII ZERO 0. ASCII value of NULL or \0 is ZERO.
7) What is the output of C program with strings.?
int main() { char str[]="ANDAMAN"; int i=0; while(str[i] != '\0') { printf("%c",str[i]); i++; } return 0; }
A) AAAAAAA
B) ANDAMAN
C) Compiler error
D) None of the above
Answer [=] B
8) What is the output of C program with strings.?
int main() { char str[]="MALDIVES"; printf("%s ",str); puts(str); return 0; }
A) MALDIVES
B) MALDIVES MALDIVES
C) M MALDIVES
D) None of the above
Answer [=] B
Explanation:
PUTS prints strings without any format specifier like %s.
9) What is the output of C program with strings.?
int main() { char str[3]="SUNDAY"; printf("%s",str); }
A) SUN
B) SUNgarbagevalues
C) compiler error
D) None of the above
Answer [=] B
Explanation:
You get C warning.
warning: initializer-string for array of chars is too long
10) Choose a correct C statement about String functions.?
A) int n=strlen(“abc”) returns 3.
B) strupr(“abc”) returns ABC
C) strlwr(“Abc”) returns abc
D) All the above
Answer [=] D
11) Choose a correct C statement about String functions.?
A) strrev(“abcD”) returns Dcba.
B) strcmp(“abc”, “bcd”) returns a negative number
C) strcmp(“234″,”123”) returns a positive number
D) All the above
Answer [=] D
12) Choose a correct C statement about String functions.?
A) toupper(‘a’) returns A
B) tolower(‘D’) returns d.
C) strcmp(“123″,”12345”) returns a negative number
D) All the above
Answer [=] D
13) What is the output of C program.?
int main() { char str1[]="JAMES,"; char str2[15]="BOND "; strcat(str2,str1); printf("%s",str2); printf("%s",str1); }
A) JAMES BOND,JAMES,
B) JAMES,JAMES,
C) BOND JAMES,JAMES,
D) None of the above
Answer [=] C
Explanation:
Here str1 is not affected by strcat function. STRCAT(destination, source).
14) What is the output of C program.?
int main() { printf("%c","HUMPTY"[2]); }
A) U
B) M
C) HUMPTY
D) None of the above
Answer [=] B
15) What is the output of C program.?
int main() { char str1[] = "FIRST"; char str2[20]; strcpy(str2,str1); printf("%s %s ",str1,str2); printf("%d", (str1!=str2)); printf("%d", strcmp(str1,str2)); return 0; }
A) FIRST FIRST 0 0
B) FIRST FIRST 1 1
C) FIRST FIRST 1 0
D) FIRST FIRST 0 1
Answer [=] C
Explanation:
STRCPY copies STR1 value to another memory location pointed by STR2. Only STR1 and STR2 values are same but not memory locations.
16) What is the output of C program with array of pointers to strings.?
int main() { char *code[]={"IN","USA","K"}; printf("%s", code[1]); return 0; }
A) IN
B) U
C) USA
D) Compiler error
Answer [=] C
Explanation:
It is an array of arrays. Using an array of pointers to strings, we can save memory. Different strings can have different lengths. Other wise, max length of any element should be the length of all elements. It wastes space.
17) What is the output of C program with String arrays.?
int main() { char code[3][4]={"IN","USA","K"}; printf("%s", code[1]); return 0; }
A) IN
B) USA
C) K
D) Compiler error
Answer [=] B
Explanation:
Here, we have not used pointers to strings. So a total of 3×4=12 bytes is allocated. Using a pointers to strings, we can allocate just 3+4+2=9 bytes. Extra byte stores NULL or \0. USA+null = 4 characters.
18) What is the output of C program with array of pointers to strings.?
int main() { char *code[2]; code[0]= (char *)malloc(4); strcpy(code[0], "IND"); printf("%s", code[0]); return 0; }
A) I
B) IN
C) IND
D) Compiler error
Answer [=] C
Explanation:
If you use a pointer to a string rather than char str[, you should allocate memory using the calloc() or malloc() functions before writing something like “IND” into it. Your results would be unexpected otherwise.
19) What is actually passed to PRINTF or SCANF functions.?
A) Value of String
B) Address of String
C) End address of String
D) Integer equivalent value of String
Answer [=] B
Explanation:
printf(“Hello”) takes the address of “Hello” and processes till it reaches NULL or \0.
20) What is the output of C program with strings.?
int main() { char *code="JUMPER"; if(code[6]=='\o') { printf("SUMMER"); } else { printf("WINTER"); } return 0; }
A) SUMMER
B) WINTER
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
SLASH O is different from SLASH ZERO. Use ‘\0’ to get end of string.