What are Arrays Strings
Arrays
-
Definition: An array is a data structure that stores a fixed-size, ordered collection of elements of the same data type. Each element in an array is identified by its index or position.
-
Features:
- Fixed Size: Arrays have a predetermined size when they are declared, and this size cannot be changed during runtime.
- Homogeneous: All elements in an array must be of the same data type (e.g., integers, floats, characters).
- Indexed: Elements in an array are accessed using an index, starting from 0 for the first element.
- Contiguous Memory: Array elements are stored in contiguous memory locations.
-
Examples:
- Integer Array:
int numbers[5];
- Character Array (String):
char greeting[10];
- Integer Array:
-
Common Operations:
- Accessing Elements:
array[index]
- Modifying Elements:
array[index] = value
- Iterating through Elements: Using loops (e.g.,
for
loop) - Finding Length:
sizeof(array) / sizeof(array[0])
- Accessing Elements:
Strings
-
Definition: A string is a sequence of characters, represented as an array of characters. In many programming languages, strings are treated as a special type of array with built-in functions for text manipulation.
-
Features:
- Variable Length: Strings can vary in length, and their size can change during runtime.
- Characters: Strings are composed of characters, typically represented using the
char
data type. - Null-Terminated: In C-style strings, a null character (
'\0'
) marks the end of the string.
-
Examples:
- C-style String:
char greeting[] = "Hello";
- String in Python:
"Hello, World"
- C-style String:
-
Common Operations:
- Concatenation: Combining two or more strings (e.g.,
"Hello, " + "World"
) - Length: Finding the number of characters in a string (e.g.,
strlen
in C/C++) - Comparison: Checking if two strings are equal (e.g.,
"abc" == "def"
) - Substring: Extracting a portion of a string (e.g.,
"Hello".substring(1, 3)
in Java)
- Concatenation: Combining two or more strings (e.g.,