Open In App

Introduction to Strings – Data Structure and Algorithm Tutorials

Last Updated : 22 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Strings are sequences of characters. The differences between a character array and a string are, a string is terminated with a special character ‘\0’ and strings are typically immutable in most of the programming languages like Java, Python and JavaScript. Below are some examples of strings:

“geeks” , “for”, “geeks”, “GeeksforGeeks”, “Geeks for Geeks”, “123Geeks”, “@123 Geeks”

How Strings are 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. String literals (assigned to pointers) are immutable in C and C++.

In C++, strings created using string class are mutable and internally represented as arrays. In Python, Java and JavaScript, strings characters are stored at contiguous locations (like arrays).

String

How to Declare Strings in various languages?

  • C: Strings are declared as character arrays or pointers and must end with a null character (\0) to indicate termination.
  • C++: Supports both C-style character arrays and the std::string class, which provides built-in functions for string manipulation.
  • Java: Strings are immutable objects of the String class, meaning their values cannot be modified once assigned.
  • Python: Strings are dynamic and can be declared using single, double, or triple quotes, making them flexible for multi-line text handling.
  • JavaScript: Strings are primitive data types and can be defined using single, double, or template literals (backticks), allowing for interpolation.
  • C#: Uses the string keyword, which represents an immutable sequence of characters, similar to Java.
  • There is no character type on Python and JavaScript and a single character is also considered as a string.

Below is the representation of strings in various languages:

C++
// C++ program to demonstrate String
// using Standard String representation

#include <iostream>
#include <string>
using namespace std;

int main()
{

    // Declare and initialize the string
    string str1 = "Welcome to GeeksforGeeks!";

    // Initialization by raw string
    string str2("A Computer Science Portal");

    // Print string
    cout << str1 << endl << str2;

    return 0;
}
C Java Python C# JavaScript PHP

Are Strings Mutable in Different Languages?

  • In C/C++, string literals (assigned to pointers) are immutable.
  • In C++, string objects are mutable.
  • In Python, Java and JavaScript, strings are immutable.
C++
#include <iostream>
using namespace std;

int main() {
    const char* str = "Hello, world!";
    str[0] = 'h';  // Error : Assignment to read only
    cout << str;
    return 0;
}
C Java Python JavaScript

General Operations performed on String

Here we are providing you with some must-know concepts of string:

  • Length of String : The length of a string refers to the total number of characters present in it, including letters, digits, spaces, and special characters. It is a fundamental property of strings in any programming language and is often used in various operations such as validation, manipulation, and comparison.
  • Search a Character : Searching for a character in a string means finding the position where a specific character appears. If the character is present multiple times, you might need to find its first occurrence, last occurrence, or all occurrences.
  • Check for Substring : Checking for a substring means determining whether a smaller sequence of characters exists within a larger string. A substring is a continuous part of a string, and checking for its presence is a common operation in text processing, search algorithms, and data validation.
  • Insert a Character : Inserting a character into a string means adding a new character at a specific position while maintaining the original order of other characters. Since strings are immutable in many programming languages, inserting a character usually involves creating a new modified string with the desired character placed at the specified position.
  • Delete a Character : Deleting a character from a string means removing a specific character at a given position while keeping the remaining characters intact. Since strings are immutable in many programming languages, this operation usually involves creating a new string without the specified character.
  • Check for Same Strings : Checking if two strings are the same means comparing them character by character to determine if they are identical in terms of length, order, and content. If every character in one string matches exactly with the corresponding character in another string, they are considered the same.
  • String Concatenation : String concatenation is the process of joining two or more strings together to form a single string. This is useful in text processing, formatting messages, constructing file paths, or dynamically creating content.
  • Reverse a String : Reversing a string means arranging its characters in the opposite order while keeping their original positions intact in the reversed sequence. This operation is commonly used in text manipulation, data encryption, and algorithm challenges.
  • Rotate a String Rotating a string means shifting its characters to the left or right by a specified number of positions while maintaining the order of the remaining characters. The characters that move past the boundary wrap around to the other side.
  • Check for Palindrome : Checking for a palindrome means determining whether a string reads the same forward and backward. A palindrome remains unchanged when reversed, making it a useful concept in text processing, algorithms, and number theory.

Introduction to Strings – Data Structure and Algorithm Tutorials – FAQs

Is string a linear data structure?

Yes, string is a linear data structure.

Where are strings used?

It is used to store the sequence of characters.

Is string a data type?

A string is generally considered a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding.

Why is text called string?

Text are also called string because it consists of sequence of characters like string.

What are characters in a string?

Each digit in a string is a character and character is a single visual object used to represent text, numbers, or symbols.

Search a Character



Next Article

Similar Reads

three90RightbarBannerImg