Posts

Showing posts with the label Strings

Strings in Python

In Python, strings are sequences of characters enclosed in either single quotes (`'`) or double quotes (`"`). They are immutable, meaning once a string is created, it cannot be changed. However, you can create new strings based on operations performed on the original string. ### Basic String Operations 1. **Creating Strings:**    ```python    single_quoted = 'Hello, World!'    double_quoted = "Hello, World!"    ```

Strings in C ++

**Strings in C++** In C++, strings can be handled in two primary ways: using **C-style character arrays** and the **`std::string`** class from the Standard Template Library (STL). Here's a structured overview: ---  **1. C-Style Strings** - **Definition**: Arrays of characters terminated by a null character (`'\0'`). - **Declaration**:   ```cpp   char cstr1[] = "Hello";  // Automatically adds '\0'   char cstr2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};   ``` - **Key Functions** (from `<cstring>`):   - `strlen(cstr)`: Returns length (excluding `'\0'`).   - `strcpy(dest, src)`: Copies `src` to `dest`.   - `strcat(dest, src)`: Appends `src` to `dest`.   - `strcmp(cstr1, cstr2)`: Compares lexicographically (returns 0 if equal). - **Pitfalls**:   - Buffer overflow risks (e.g., `strcpy` without checking sizes).   - Manual memory management required. --- **2. `std::string` Class** - **Header**: `<string...

Strings in the C#

Strings in C# In C#, strings are sequences of characters enclosed in double quotes (""). They are represented by the string keyword and are immutable, meaning their value cannot be changed after creation. Creating Strings: string greeting = "Hello, world!"; Accessing Characters: You can access individual characters using index-based access: char firstChar = greeting[0]; // 'H' String Length: To get the length of a string, use the Length property: int length = greeting.Length; // 13 String Concatenation: You can combine strings using the + operator or the Concat method: string firstName = "Alice"; string lastName = "Johnson"; string fullName = firstName + " " + lastName; // "Alice Johnson" string fullName2 = string.Concat(firstName, " ", lastName); // "Alice Johnson" String Comparison: You can compare strings using the == and != operators, or the Equals method: string str1 = "Hello"; string...