Strings in C and Char Arrays Tutorial and MCQ
Table of Contents
Learn Strings in C and Char Arrays Tutorial 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.
Strings Char 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 ?
In practice, you will be using String Library Functions to know Length of a String, Copy Strings, Concatenate Strings and more. All these functions want to know after what character string ends in memory. This NULL character tells these string functions about end of a string.
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.
Strings 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.
Strings in C 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 in C examples
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
Strings in C
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
String Assignments in c
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 37]
Strings in C MCQ
1) What is a String in C Language.?
A) String is a new Data Type in C
B) String is an array of Characters with null character as the last element of array.
C) String is an array of Characters with null character as the first element of array
D) String is an array of Integers with 0 as the last element of array.
Answer [=] B
2) Choose a correct statement about C String.
char ary[]="Hello..!";
A) Character array, ary is a string.
B) ary has no Null character at the end
C) String size is not mentioned
D) String can not contain special characters.
Answer [=] A
Explanation:
It is a simple way of creating a C String. You can also define it like the below. \0 is mandatory in this version. char ary[] = {‘h’,’e’,’l’,’l’,’o’,’\0′};
3) What is the Format specifier used to print a String or Character array in C Printf or Scanf function.?
A) %c
B) %C
C) %s
D) %w
Answer [=] C
Explanation:
char ary[]="Hello..!"; printf("%s",ary);
4) What is the output of C Program with Strings.?
int main() { char ary[]="Discovery Channel"; printf("%s",ary); return 0; }
A) D
B) Discovery Channel
C) Discovery
D) Compiler error
Answer [=] B
Explanation:
%s prints the while character array in one go.
5) What is the output of C Program with Strings.?
int main() { char str[]={'g','l','o','b','e'}; printf("%s",str); return 0; }
A) g
B) globe
C) globe\0
D) None of the above
Answer [=] D
Explanation:
Notice that you have not added the last character \0 in the char array. So it is not a string. It can not know the end of string. So it may print string with some garbage values at the end.
6) What is the output of C Program with Strings.?
int main() { char str[]={'g','l','o','b','y','\0'}; printf("%s",str); return 0; }
A) g
B) globe
C) globe\0
D) Compiler error
Answer [=] B
Explanation:
Adding a NULL or \0 at the end is a correct way of representing a C string. You can simple use char str[]=”globy”. It is same as above.
7) How do you convert this char array to string.?
char str[]={'g','l','o','b','y'};
A) str[5] = 0;
B) str[5] = ‘\0’
C) str[]={‘g’,’l’,’o’,’b’,’y’,’\0′};
D) All the above
Answer [=] D
8) What is the output of C Program.?
int main() { int str[]={'g','l','o','b','y'}; printf("A%c ",str); printf("A%s ",str); printf("A%c ",str[0]); return 0; }
A) A A A
B) A Ag Ag
C) A*randomchar* Ag Ag
D) Compiler error
Answer [=] C
Explanation:
Notice that STR is not a string as it is not a char array with null at the end. So STR is the address of array which is converted to Char by %c. If you use %s, it prints the first number converted to char.
9) What is the output of C Program with arrays.?
int main() { char str[]={"C","A","T","\0"}; printf("%s",str); return 0; }
A) C
B) CAT
C) CAT\0
D) Compiler error
Answer [=] D
Explanation:
Yes. You can not use Double Quotes ” to represent a single character. Correct way is ‘C‘ not “C“. You should use Single Quotes around a single character constant.
10) What is the maximum length of a C String.?
A) 32 characters
B) 64 characters
C) 256 characters
D) None of the above
Answer [=] D
Explanation:
Maximum size of a C String is dependent on implemented PC memory. C does not restrict C array size or String Length.
11) What is the output of C program with strings.?
int main() { char str1[]="JOHN"; char str2[20]; str2= str1; printf("%s",str2); return 0; }
A) JOHN
B) J
C) JOHN\0
D) Compiler error
Answer [=] D
Explanation:
You can not assign one string to the other. It is an error. “
error: assignment to expression with array type
12) What is the output of C Program with arrays.?
int main() { char str[25]; scanf("%s", str); printf("%s",str); return 0; } //input: South Africa
A) South
B) South Africa
C) S
D) Compiler error
Answer [=] A
Explanation:
SCANF can not accept a string with spaces or tabs. So SCANF takes only South into STR.
13) What is the output of C program with strings.?
int main() { char str[2]; scanf("%s", str); printf("%s",str); return 0; } //Input: South
A) So
B) South
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
In C Arrays, Overflow or Out of Bounds is not checked properly. It is your responsibility to check.
14) What is the output of C Program with strings.?
int main() { char str[2]; int i=0; scanf("%s", str); while(str[i] != '\0') { printf("%c", str[i]); i++; } return 0; } //Input: KLMN
A) KL
B) KLMN
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
It always overwrites the next memory locations of the array. It is your responsibility to check bounds. Scanf automatically adds a ‘\0’ at the end of entered string.
15) What is the output of C Program with String Pointer.?
int main() { char country[]="BRAZIL"; char *ptr; ptr=country; while(*ptr != '\0') { printf("%c", *ptr); ptr++; } return 0; }
A) B
B) BRAZIL
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
*ptr != ‘\0’ is the main part of traversing a C String.
16) How do you accept a Multi Word Input in C Language.?
A) SCANF
B) GETS
C) GETC
D) FINDS
Answer [=] B
Explanation:
Yes. gets(str) fills the array str with the input given by the user.
17) Choose a correct C Statement about Strings.
A) PRINTF is capable of printing a multi word string.
B) PUTS is capable of printing a multi word string.
C) GETS is capable of accepting a multi word string from console or command prompt
D) All the above
Answer [=] D
18) What is the output of C Program with String Pointers.?
int main() { char *p1 = "GOAT"; char *p2; p2 = p1; printf("%s", p2); }
A) G
B) GOAT
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Yes. You can assign one String pointer to another. But you can not assign a normal character array variable to other like STR2 = STR1. It is an error.
19) What is the output of C Program with String arrays.?
int main() { char *p1 = "GOAT"; char *p2; p2 = p1; p2="ANT"; printf("%s", p1); }
A) ANT
B) GOAT
C) G
D) A
Answer [=] B
Explanation:
*p1 and *p2 are completely pointing different memory locations. So, p1 value is not touched.
20) What is the output of C Program with String Arrays.?
int main() { char p[] = "GODZILLA"; int i=0; while(p[i] != '\0') { printf("%c",*(p+i)); i++; } }
A) G
B) GODZILLA
C) Compiler error
D) None of the above
Answer [=] B
Explanation:
Notice the usage of *(p+i). Remember that, p[i] == *(p+i) == *(i+p) == i[p]