Interview Questions on Strings (C Language)

📘 Basic String Concepts – Short Answer Interview Questions

  1. How are strings represented in C?
    As null-terminated character arrays ('\0' at end).
  2. What is the difference between char str[] and char *str?
    char str[] is an array, char *str is a pointer. Array size can't change.
  3. How do you find the length of a string without using strlen()?
    Loop until null terminator: while(str[i] != '\0') i++;
  4. What happens if you forget the null terminator in a string?
    String functions will read beyond allocated memory, causing undefined behavior.
  5. Difference between strcpy() and strncpy()?
    strncpy() allows specifying max characters to copy, safer against buffer overflows.
  6. What does sizeof() return for a string literal?
    Returns size including null terminator (e.g., sizeof("abc") is 4).
  7. How do you reverse a string in place?
    Swap characters from ends towards center using two pointers.
  8. What is the difference between strcmp() and strncmp()?
    strncmp() compares only first n characters.
  9. Can a string literal be modified in C?
    No. Attempting to modify string literals causes undefined behavior.
  10. How do you concatenate two strings without using strcat()?
    Find end of first string, then copy second string starting at that position.
  11. How is memory allocated for string literals vs character arrays?
    String literals: usually in read-only segment. Character arrays: stack or heap.
  12. Explain the difference between str[i] and *(str + i).
    They are functionally equivalent due to pointer arithmetic.
  13. How do you initialize a string with all spaces?
    Use char str[10] = " "; or loop to set each character.
  14. Can you assign one string to another directly using = operator?
    No. Use strcpy() or copy characters individually.
  15. What is the use of const in const char *str?
    Prevents modification of string contents through this pointer.
  16. How do you sort an array of strings without using library functions?
    Use basic sorting algorithms with strcmp() for comparison.
  17. What is the role of static keyword when used with strings inside a function?
    Preserves string content between function calls.
  18. What is the difference between malloc and calloc for strings?
    calloc initializes memory to zero; malloc does not.
  19. What does the realloc function do for strings?
    Resizes dynamically allocated string memory while preserving content.
  20. How do you pass a substring to a function?
    Pass pointer to starting character and optionally length: func(&str[start]).
  21. What is pointer arithmetic and how is it related to strings?
    Pointer arithmetic lets you navigate string characters using pointer offsets.
  22. How do you implement string search (substring search)?
    Use nested loops to compare pattern with every possible substring position.
  23. How do you find duplicate characters in a string?
    Use hash table or array to count character frequencies.
  24. What is a dynamic string and how is it implemented in C?
    String created using malloc/realloc to grow/shrink at runtime.
  25. How do you rotate a string by k characters?
    Use reversal technique or temporary storage.
  26. Can strings be returned from functions in C?
    Return pointer to string literal or dynamically allocated memory.
  27. What's the difference between shallow and deep copying strings?
    Shallow copy copies pointer only; deep copy duplicates string content.
  28. How do you merge two sorted strings into one?
    Use two-pointer technique to merge efficiently in O(n).
  29. What is the difference between stack and heap allocation of strings?
    Stack: fixed size, fast. Heap: dynamic size, must be manually managed.
  30. 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

  1. How do you tokenize a string in C?
    Use strtok() function with delimiters.
  2. What is the memory layout of a string array in C?
    Array of pointers, each pointing to a null-terminated string.
  3. Difference between char str[3][4] and char **str?
    First is a contiguous 2D array; second is pointer to pointer, may not be contiguous.
  4. Can you dynamically allocate an array of strings? How?
    Yes. Allocate array of pointers, then allocate each string individually.
  5. How do you find the longest common prefix among an array of strings?
    Compare characters at each position until mismatch found.
  6. What is the difference between ASCII and Unicode strings?
    ASCII uses 1 byte per char; Unicode needs more bytes for non-English chars.
  7. How do you implement your own atoi() function?
    Process each digit character, build integer value: result = result * 10 + (str[i] - '0').
  8. How do you print all permutations of a string?
    Use backtracking/recursion to swap characters and generate permutations.
  9. How to find the first non-repeating character in a string?
    Use hash table to count occurrences, then scan string for count=1.
  10. How do you pass an array of strings to a function?
    As char *arr[] or char **arr parameter.
  11. Can you return an array of strings from a function?
    Return pointer to dynamically allocated array of strings.
  12. How do you implement string compression (e.g., "aaabbb" → "a3b3")?
    Count consecutive characters and build compressed string.
  13. What happens if you access string index out of bounds?
    Leads to undefined behavior or segmentation fault.
  14. What is a string pool and how does it work?
    Memory area where identical string literals may share storage.
  15. How do you initialize an array of strings with empty strings?
    Use char arr[N][M] = {""}; or loop to set each string to "\0".
  16. How do you check if a string is a palindrome?
    Compare characters from ends towards center.
  17. How do you reverse words in a string (e.g., "hello world" → "world hello")?
    Reverse entire string, then reverse each word individually.
  18. How to compute hash value for a string?
    Use polynomial rolling hash: hash = (hash * p + char) % m.
  19. What are common pitfalls in string manipulation?
    Buffer overflows, missing null terminators, memory leaks, encoding issues.
  20. What is the space complexity of storing N strings?
    O(total characters across all strings + pointers for each string)