Strings In Data Structures

Megha Agarwal
3 min readJan 5, 2021



Nowadays, computers are widely used for word processing applications such as creating, inserting, updating, and modifying textual data. Besides this, we need to search for a particular pattern within a text, delete it, or replace it with another pattern. So, there is a lot that we as users do to manipulate the textual data.

What are Strings?

A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. It is comprised of a set of characters that can also contain spaces and numbers.

Internally, every C++ program assigns null character ‘/0 ’ as the last character of every string. This indicates the end of the string and it means if you want to store a 5 character string in an array then you must define array size of 6 as a good practice, though C++ does not complain about it.

String Declaration:

A string, as it is simply an array of characters, is declared as:

char name[20];

  • This declares a string named name which can hold strings from length 0 to length 19.

Strings in Memory:

  • All strings must end with a null character. When a string is enclosed in double quotes, i.e. “Hello there”, a null character is automatically inserted.
  • The double quotes mean “the string is whatever is inside the quotes followed by a null character.”

String Initialization:

  1. char pet[5] = { ‘l’, ‘a’, ‘m’, ‘b’, ‘\0’ } ;

2. char pet[5] ;
pet[0] = ‘l’ ; pet[1] = ‘a’ ; pet[2] = ‘m’ ;
pet[3] = ‘b’ ; pet[4] = ‘\0’ ;

3. char pet[5] = “lamb” ;

All the above are equivalent. Strings can be initialized through any of the above shown method!

Strings can also be initialized using a string constant, in this form:

char string[20] = “Hello there”;

So, what does this string look like in memory???

String Representation:

Character arrays holding strings must have room for ‘\0’ following the actual data The empty string “” occupies 1 char.


String Library: <string.h>:

C++ provides a number of routines to manipulate strings, through library functions. Standard C++ includes a library of string functions.

–use #include <string.h>

String Functions :

The most common functions used from the library are:
1. strlen(“name of string”)
2. strcpy( dest, source)
3. strcmp( string1, string2 )
4. strstr( str1, str2 )

strlen( string ):- This library function tells the length of string.

strcpy( dest, source):- This function copies the string “source” to string “dest”.

strcmp( str1, str2 ):- This function is used to compare two strings “str1” and “str2”. this function returns zero(“0”) if the two compared strings are equal, else some none zero integer.

strstr(str1, str2):- This library function finds the first occurrence of the substring str2 in the string str1. The terminating ‘\0’ character is not compared.

Some of the other library function for string manipulation are as follows:

strncpy :- strncpy is similar to strcpy, but it allows the number of characters to be copied to be specified.

strncat :- This function appends at most N characters from the source string to the end of the destination string. This function returns a pointer to the destination string, or a NULL pointer on error.

strncmp :- This function compares the first N characters of each string. If the first string is greater than the second, it returns a number greater than zero. If the second string is greater, it returns a number less than zero. If the strings are equal, it returns 0.

memset :- memset is useful to initialize a string to all nulls, or to any character.

strtok :- The strtok function is used to find the next token in a string. The token is specified by a list of possible delimiters.