Skip to main content

A Very Useful Data Structure i.e. String

Β· 2 min read
Rishi Verma
B.Tech(CSE) STUDENT, Developer, Tech Enthusiast , Competitive Programmer

In data structures, a string is a sequence of characters used to represent text. Strings are commonly used for storing and manipulating textual data in computer programs. They can be manipulated using various operations like concatenation, substring extraction, and comparison.

What is String?​

In Data Structure, a string is a data type used to represent a sequence of characters. Strings can include letters, numbers, symbols, and spaces. They are often used to represent text, such as words, sentences, or any collection of readable characters.

Below are Some Examples of String:

"geeks","for","geeks""GeeksforGeeks" ,"Geeks for Geeks","123Geeks","@123 Geeks"

How String is Represented:​

In C, a string can be referred to either using a character pointer or as a character array. Here are some important points:

  • Character Array: When strings are declared as character arrays, they are stored like other types of arrays in C.

    char str[] = "Hello, World!";
    • If str[] is an auto variable, the string is stored in the stack segment.
    • If str[] is a global or static variable, it is stored in the data segment.
  • Character Pointer: Strings can also be referred to using a character pointer.

    char *str = "Hello, World!";
    • The string literal is stored in the read-only section of memory, and str points to this location.

Understanding these storage mechanisms is crucial for efficient memory management and avoiding common pitfalls like buffer overflows.

How String is represented in Memory:​

In C, a string can be referred to either using a character pointer or as a character array. When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is an auto variable then the string is stored in the stack segment, if it’s a global or static variable then stored in the data segment, etc.


Declaration of Strings:​

In various programming languages, strings can be declared in different ways. Here are some examples:

  • C++:

    std::string str = "Hello, World!";
  • Java:

    String str = "Hello, World!";
  • Python:

    str = "Hello, World!"

Each language has its own syntax and methods for handling strings, but the fundamental concept remains the same: a string is a sequence of characters.