
Strings Programming Practice
"Strings are similar to arrays. It is no exaggeration to call them array's cousins. Attempt the following questions to get well on this topic."
— All the best
Easy
-
Consider the following array of pointers to character strings (C-style strings).
char *titles[] = { "Data structures in C", "Algorithms with C++", "Python and Django", "Web development basics" };
- Calculate the total memory (in bytes) occupied by the array of pointers itself (assuming a pointer size of 8 bytes on a 64-bit system).
- If these same four strings were stored in a single two-dimensional character array (e.g.,
char matrix[ROWS][COLS]
), how much total memory would be required to hold the string data? (Assume the maximum possible length is used for all strings in the 2D array approach).
- Is it a suitable and safe practice in C to use a statically-sized array of string pointers (like
char *list[10];
) to read an unknown number of strings from a user's keyboard input? Explain your reasoning, focusing on memory allocation and security. - Implement a function that uses a Brute-Force searching algorithm to find the starting index of a given pattern string within a larger text string. The search must be conducted in a non-standard way: the comparison logic should start from the right end of the text and scan backward toward the beginning.
- Write a procedure which counts the number NUM of times the word “the” appears in the short story S. (We do not count “the” in “mother,” and we assume no sentence ends with the word “the.”)
- Write a program that takes two strings as input: a main sentence and a target_word. The program should first check if the target_word exists within the sentence. If it does, your function must then remove the first occurrence of that target_word from the sentence and print the resulting string.
- Create a program that processes an input string and removes any excess whitespace. The final string should contain no leading or trailing spaces, and all internal words must be separated by exactly one space.
Example: If the input is" This string has too much space. "
, the output should be"This string has too much space."
. - Develop a utility that allows a user to input a primary document string and two separate words: an old_word and a new_word. The program should find all occurrences of the old_word within the document and replace them with the new_word. The original document string should be modified in place or a new modified string should be returned.
- Design a program that accepts a multi-line block of text from the user (or reads from a file) and provides three distinct metrics for that text:
Total number of characters. Total number of words. Total number of lines (including the final line).
- Write a function that counts the total number of times a specific character, say the lowercase letter 'a', appears across all the strings stored in the following array of pointers:
char *quotes[] = { "An array of pointers is powerful.", "Character counting is a classic task.", "Learning advanced concepts", "Always practice coding", "a and b and c" };
Note that the word “the” can appear as THE followed by a space at the beginning of a line, as a space followed by THE at the end of a line, or as a space followed by THE followed by another space elsewhere in a line. Hence we must check these three cases for each line.