Introduction to Strings – Data Structure and Algorithm Tutorials
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).

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++ 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 program to illustrate strings
#include <stdio.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s", str);
return 0;
}
// Java code to illustrate String
import java.io.*;
import java.lang.*;
class Test {
public static void main(String[] args)
{
// Declare String without using new operator
String s = "GeeksforGeeks";
// Prints the String.
System.out.println("String s = " + s);
// Declare String using new operator
String s1 = new String("GeeksforGeeks");
// Prints the String.
System.out.println("String s1 = " + s1);
}
}
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
# Creating a String
# with triple Quotes
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
# Creating String with triple
# Quotes allows multiple lines
String1 = '''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
// Include namespace system
using System;
public class Test
{
public static void Main(String[] args)
{
// Declare String without using new operator
var s = "GeeksforGeeks";
// Prints the String.
Console.WriteLine("String s = " + s);
// Declare String using new operator
var s1 = new String("GeeksforGeeks");
// Prints the String.
Console.WriteLine("String s1 = " + s1);
}
}
// Declare and initialize the string
let str1 = "Welcome to GeeksforGeeks!";
// Initialization using another method
let str2 = new String("A Computer Science Portal");
// Print strings
console.log(str1);
console.log(str2.toString());
<?php
// single-quote strings
$site = 'Welcome to GeeksforGeeks';
echo $site;
?>
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.
#include <iostream>
using namespace std;
int main() {
const char* str = "Hello, world!";
str[0] = 'h'; // Error : Assignment to read only
cout << str;
return 0;
}
#include <stdio.h>
int main() {
char *str = "Hello, world!";
str[0] = 'h'; // Undefined behavior
printf("%s\n", str);
return 0;
}
// Java Program to demonstrate why
// Java Strings are immutable
import java.io.*;
class GfG {
public static void main(String[] args) {
String s1 = "java";
s1.concat(" rules");
// s1 is not changed because strings are
// immutable
System.out.println("s1 refers to " + s1);
}
}
# Create an immutable string
s = "GFG"
# This will cause a error
# because strings are immutable
s[1] = 'f'
print(s)
let str = "GFG";
str[1] = "f";
console.log(str); // Output: "GFG" (unchanged)
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.