Strings Explained: Memory, Operations, Worked Traces and Exam Patterns

Build a precise string memory model, trace an in-place update step by step, and learn the boundaries and common question forms that cause mistakes.

KnowledgeGate Team

Exam prep & CS education

Updated 7 Nov 20266 min read
Strings Explained: Memory, Operations, Worked Traces and Exam Patterns

A C string is an indexed character sequence terminated by a null character. Memory cells, an append-and-reverse trace, and exam cases expose the boundaries between characters, arrays, pointers, length and capacity. Zero to Hero complete CS course provides a structured route.

1. What a string is, and what it is not

A string is an ordered sequence of characters. In GATE, logical indices 0, 1, 2 and 3 contain G, A, T and E, so the logical length is 4.

That definition is language-neutral. The memory traces and syntax below use C because this is a Programming subject note. In C, GATE needs a terminating null character, so its minimal array uses five bytes: [G,A,T,E,\0]. The empty string "" has logical length 0, but still occupies one byte for \0.

Do not confuse 'A' with "A". The first is one character value. The second is a C string stored as [A,\0]. Similarly, an arbitrary character array is not automatically a string. It must contain a valid terminator within its bounds.

2. Length, capacity and the C memory layout

Consider three declarations:

  • char a[] = "CAT"; allocates 4 bytes and has length 3.

  • char b[6] = "CAT"; stores [C,A,T,\0,\0,\0]. Its allocated size is 6, its current length is 3, and its maximum visible payload is 5 characters.

  • const char *p = "CAT"; points to a string literal. It must not be treated as writable storage.

For b, the three relevant answers are strlen(b) = 3, sizeof(b) = 6 in its declaring scope, and payload capacity 5. The terminator consumes storage but is not counted in the logical length.

C string memory strip for char s[9] = "GATE": indices 0 through 8, GATE followed by null cells, with index 4 highlighted as the terminator.

3. Worked trace: append 2 and 6, then reverse in place

Here the array can hold eight visible characters. It starts with four, and two appends still leave a cell for the terminator.

c
char s[9] = "GATE";
int n = 4;

s[n++] = '2';
s[n++] = '6';
s[n] = '\0';

for (int i = 0, j = n - 1; i < j; ++i, --j) {
    char temp = s[i];
    s[i] = s[j];
    s[j] = temp;
}

Initially, n = 4, the visible string is GATE, and the cells are [G,A,T,E,\0,\0,\0,\0,\0]. Appending 2 writes index 4; n becomes 5, giving GATE2 and [G,A,T,E,2,\0,\0,\0,\0]. Appending 6 writes index 5; n becomes 6, and writing \0 at index 6 gives GATE26 and [G,A,T,E,2,6,\0,\0,\0].

The reverse loop swaps (0,5) to produce 6ATE2G, then (1,4) to produce 62TEAG, and finally (2,3) to produce 62ETAG. After the update, i = 3 and j = 2, so the loop stops. The printed string is 62ETAG, strlen(s) = 6, sizeof(s) = 9, and the loop performs three swaps. The furthest write used for termination is index 6, safely inside indices 0 through 8.

Append-and-reverse trace for char s[9] = "GATE" showing six stages, n values, swaps, visible strings, cell contents, and final strlen 6, sizeof 9, swaps 3.

4. Core string operations and their costs

The main operation families become clearer with values. LEVEL has length 5. Copying CAT needs destination cells [C,A,T,\0]. Concatenating DATA and BASE produces DATABASE, length 8. Comparing CAT with CAT gives equality, written as strcmp(...) == 0 in C. Taking positions 1 through 3 from GATECS gives ATE.

Operation

Cost

Index a known position

O(1)

Scan for length

O(n)

Copy n characters

O(n)

Compare strings

Worst-case O(min(n,m))

Reverse in place

O(n) time, O(1) extra space

Naive pattern search

Worst-case O((n-m+1)m)

These costs assume a contiguous, null-terminated representation. Indexing is constant-time because the address is formed from the base plus an index; strlen, copying and comparison scan until a terminator or a mismatch. Reversing uses two indices and one temporary character, so its extra storage stays constant even as the string grows.

Repeated append can expose a second cost. If each append first calls strlen to find the end, a sequence of single-character appends may rescan the existing payload and take quadratic total time. Tracking the current length avoids that scan, but it does not remove the need to check capacity for the new character and its terminating \0.

A parameter written as char s[9] is adjusted to a pointer in a function declaration, so the bound is documentation rather than runtime storage. Pass the capacity explicitly and keep the caller-owned array writable; a pointer to a literal has no such permission.

For text ABABA and pattern ABA, matches begin at indices 0 and 2. The second overlaps the first, but it is still a valid match. Library functions do not remove capacity obligations: any copy or append needs enough destination space for every visible character plus one terminator.

5. Arrays, pointers and function parameters

In void revise(char s[], size_t capacity), the parameter s is received as a pointer. Therefore, sizeof(s) inside the function is the size of a pointer, not the size of the caller's array. For the worked array, call revise(s, 9) so the function receives the actual capacity separately. The article on why arrays decay when passed to functions develops this boundary further.

Now take char a[6] = "GATE";. In the declaring scope, sizeof(a) = 6 and strlen(a) = 4. After char *q = a;, both q[2] and *(q + 2) read T. However, sizeof(q) describes the pointer object. It is neither the string length nor the array capacity.

6. Traps that turn valid-looking code into a wrong answer

  • Using '0' for \0: '0' is a visible digit. Use \0 to terminate the string.

  • Forgetting the extra byte: n visible characters need at least n + 1 cells. Reserve the last one for \0.

  • Treating sizeof as strlen: sizeof reports object storage in the declaring scope; strlen scans until the terminator.

  • Changing a literal: do not write through a pointer to a string literal. Use a writable array when mutation is required.

  • Reading a phrase with scanf("%s", ...): %s stops at whitespace. Use a bounded line-input method when spaces belong to the input.

  • Appending before checking capacity: confirm that the new payload and its terminator fit before writing.

For example, char bad[4] = "GATE"; contains four characters but no terminator. It can serve as a four-element character array, but passing it to a C string function that searches for \0 is invalid. Use char good[5] = "GATE"; for a valid string.

The worked s[9] has indices 0 through 8, so at most eight visible characters fit. One cell must remain \0. Never infer a predictable result from an out-of-bounds write.

7. How exams turn string basics into questions

Exam questions commonly test four forms: counting memory cells and terminators, separating sizeof from strlen, tracing loop mutations, and calculating complexity or substring matches.

Check these three cases:

  1. For char a[6] = "GATE";, sizeof(a) = 6 and strlen(a) = 4.

  2. For char s[5] = "CODE"; s[0] = s[3]; s[3] = 'C';, the states are CODE -> EODE -> EODC, so the printed result is EODC.

  3. In text ABABA, all matches of ABA start at indices 0 and 2.

Pointer traversal and dereferencing belong with the earlier C String Pointers: Memory, Dereferencing and Traps post. Storage capacity, null termination, operation costs and append/reverse traces require the memory and mutation model used in these examples.

8. The short version and the next step

Keep five facts together:

  1. A C string ends at \0.

  2. Length excludes the terminator.

  3. Capacity includes every allocated cell.

  4. Array size and pointer size answer different questions.

  5. Every write must preserve both bounds and termination.

For a reusable exam method, draw indexed cells, mark the current terminator, update the length after each write, and trace one mutation per row. Continue with the C Language course for C concepts and coding practice, or use GATE, PSU, Engineering & Technical Exams to choose a subject-wise study path.