Interview Questions on Strings (C Language)
📘 Basic String Concepts – Short Answer Interview Questions
-
How are strings represented in C?
As null-terminated character arrays (
'\0'at end). -
What is the difference between
char str[]andchar *str?char str[]is an array,char *stris a pointer. Array size can't change. -
How do you find the length of a string without using strlen()?
Loop until null terminator:
while(str[i] != '\0') i++; -
What happens if you forget the null terminator in a string?
String functions will read beyond allocated memory, causing undefined behavior.
-
Difference between
strcpy()andstrncpy()?strncpy()allows specifying max characters to copy, safer against buffer overflows. -
What does
sizeof()return for a string literal?Returns size including null terminator (e.g., sizeof("abc") is 4). -
How do you reverse a string in place?
Swap characters from ends towards center using two pointers.
-
What is the difference between
strcmp()andstrncmp()?strncmp()compares only first n characters. -
Can a string literal be modified in C?
No. Attempting to modify string literals causes undefined behavior.
-
How do you concatenate two strings without using strcat()?
Find end of first string, then copy second string starting at that position.
-
How is memory allocated for string literals vs character arrays?
String literals: usually in read-only segment. Character arrays: stack or heap.
-
Explain the difference between
str[i]and*(str + i).They are functionally equivalent due to pointer arithmetic. -
How do you initialize a string with all spaces?
Use
char str[10] = " ";or loop to set each character. -
Can you assign one string to another directly using
=operator?No. Usestrcpy()or copy characters individually. -
What is the use of
constinconst char *str?Prevents modification of string contents through this pointer. -
How do you sort an array of strings without using library functions?
Use basic sorting algorithms with
strcmp()for comparison. -
What is the role of
statickeyword when used with strings inside a function?Preserves string content between function calls. -
What is the difference between
mallocandcallocfor strings?callocinitializes memory to zero;mallocdoes not. -
What does the
reallocfunction do for strings?Resizes dynamically allocated string memory while preserving content. -
How do you pass a substring to a function?
Pass pointer to starting character and optionally length:
func(&str[start]). -
What is pointer arithmetic and how is it related to strings?
Pointer arithmetic lets you navigate string characters using pointer offsets.
-
How do you implement string search (substring search)?
Use nested loops to compare pattern with every possible substring position.
-
How do you find duplicate characters in a string?
Use hash table or array to count character frequencies.
-
What is a dynamic string and how is it implemented in C?
String created using malloc/realloc to grow/shrink at runtime.
-
How do you rotate a string by k characters?
Use reversal technique or temporary storage.
-
Can strings be returned from functions in C?
Return pointer to string literal or dynamically allocated memory.
-
What's the difference between shallow and deep copying strings?
Shallow copy copies pointer only; deep copy duplicates string content.
-
How do you merge two sorted strings into one?
Use two-pointer technique to merge efficiently in O(n).
-
What is the difference between stack and heap allocation of strings?
Stack: fixed size, fast. Heap: dynamic size, must be manually managed.
-
What is the time complexity of inserting a character in the middle of a string?
O(n) due to shifting of subsequent characters.
📗 Advanced String Manipulation – Short Answer Interview Questions
-
How do you tokenize a string in C?
Use
strtok()function with delimiters. -
What is the memory layout of a string array in C?
Array of pointers, each pointing to a null-terminated string.
-
Difference between
char str[3][4]andchar **str?First is a contiguous 2D array; second is pointer to pointer, may not be contiguous. -
Can you dynamically allocate an array of strings? How?
Yes. Allocate array of pointers, then allocate each string individually.
-
How do you find the longest common prefix among an array of strings?
Compare characters at each position until mismatch found.
-
What is the difference between ASCII and Unicode strings?
ASCII uses 1 byte per char; Unicode needs more bytes for non-English chars.
-
How do you implement your own
atoi()function?Process each digit character, build integer value:result = result * 10 + (str[i] - '0'). -
How do you print all permutations of a string?
Use backtracking/recursion to swap characters and generate permutations.
-
How to find the first non-repeating character in a string?
Use hash table to count occurrences, then scan string for count=1.
-
How do you pass an array of strings to a function?
As
char *arr[]orchar **arrparameter. -
Can you return an array of strings from a function?
Return pointer to dynamically allocated array of strings.
-
How do you implement string compression (e.g., "aaabbb" → "a3b3")?
Count consecutive characters and build compressed string.
-
What happens if you access string index out of bounds?
Leads to undefined behavior or segmentation fault.
-
What is a string pool and how does it work?
Memory area where identical string literals may share storage.
-
How do you initialize an array of strings with empty strings?
Use
char arr[N][M] = {""};or loop to set each string to "\0". -
How do you check if a string is a palindrome?
Compare characters from ends towards center.
-
How do you reverse words in a string (e.g., "hello world" → "world hello")?
Reverse entire string, then reverse each word individually.
-
How to compute hash value for a string?
Use polynomial rolling hash:
hash = (hash * p + char) % m. -
What are common pitfalls in string manipulation?
Buffer overflows, missing null terminators, memory leaks, encoding issues.
-
What is the space complexity of storing N strings?
O(total characters across all strings + pointers for each string)
Related String Resources