Introduction to Strings Data Structure and Algorithm Tutorials (original) (raw)

Last Updated : 22 Feb, 2025

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?

**Below is the representation of strings in various languages:

C++ `

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

#include #include 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

// 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

// 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

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)

C#

// 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); } }

JavaScript

// 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

`

Are Strings Mutable in Different Languages?

#include using namespace std;

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

C

#include <stdio.h>

int main() { char *str = "Hello, world!"; str[0] = 'h'; // Undefined behavior printf("%s\n", str); return 0; }

Java

// 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);
}

}

Python

Create an immutable string

s = "GFG"

This will cause a error

because strings are immutable

s[1] = 'f'

print(s)

JavaScript

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:

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