Appending Characters to Strings in C++
Learn efficient ways to append characters to C++ strings using push_back, +=, append, and +. Compare time complexity, performance, and memory usage for optimal string manipulation.
Well we know that decimal numbers are stored in the computer memory as binary format. For instance 7
is stored as 0111
in memory. Have you ever wondered how does characters like English letters stored in memory?
We will uncover all this in this article post.
Table of contents [Show]
Well, character are stored in computer memory binary format as well, but their storage process is different from that of numbers. Each character, like a letter, number, or symbol, is assigned a unique numeric code through character encoding standards like ASCII (American Standard Code for Information Interchange) or Unicode.
In C/C++, when we declare variables like int integer = 65;
and char alphabet = 'A';
, the compiler determines the type of data (integer or character) based on the variable’s declaration, and this influences how the data is stored in memory.
int integer = 65;
char alphabet = 'a';
Here's what happens in each case:
int integer = 65;
):integer
as an int
type, which is stored as a binary number in memory.00000000 00000000 00000000 01000001
in a 32-bit system) and stored in memory.char alphabet = ‘A’;
):alphabet
as a char
type, so it interprets 'A'
as a character rather than a numerical value.'A'
, the compiler refers to the ASCII encoding, where 'A'
corresponds to the decimal value 65.01000001
for an 8-bit char
) and stored in memory.Character encoding schemes define how characters are represented as binary data in computers.
ASCII is a character encoding standard that was developed to represent text in computers, communications equipment, and other devices that use text. ASCII assigns a unique numeric code to each character, symbol, or control command, allowing computers to store and process text data.
Here’s a summary of the key ASCII character ranges:
Decimal | Binary | Character | Description |
---|---|---|---|
0 | 00000000 | NUL | Null |
9 | 00001001 | TAB | Horizontal Tab |
10 | 00001010 | LF | Line Feed (New Line) |
13 | 00001101 | CR | Carriage Return |
32 | 00100000 | (space) | Space |
48-57 | 00110000- | 0-9 | Numbers |
65-90 | 01000001- | A-Z | Uppercase Letters |
97-122 | 01100001- | a-z | Lowercase Letters |
127 | 01111111 | DEL | Delete |
Character Encoding: ASCII values are used directly for character representation. For instance:
char letter = 'A'; // Stored as 65 in ASCII (binary: 01000001)
Unicode is a universal character encoding standard that assigns unique codes to characters, symbols, and scripts from all languages and writing systems, as well as technical symbols, emojis, and more. It was developed to overcome the limitations of ASCII and other early encoding systems, allowing consistent representation of text across different platforms and languages.
U+XXXX
, where XXXX
is a hexadecimal number. For example, U+0041
is the code point for the letter ‘A’.U+0000
to U+10FFFF
, which allows for over a million unique codes.U+0000
to U+FFFF
.Unicode can be encoded in various ways, depending on how many bytes are needed to store each character. The three primary encoding forms are UTF-8, UTF-16, and UTF-32:
01000001
(1 byte)11100010 10000010 10101100
(3 bytes)11110000 10011111 10011000 10000010
(4 bytes)00000000 01000001
(2 bytes)00100000 10101100
(2 bytes)11011000 00111110 11011111 10000010
(4 bytes)00000000 00000000 00000000 01000001
00000000 00000000 00100000 10101100
00000000 00000001 11110110 00000010
To convert a character representing a digit (like '7'
) into its corresponding integer value, we need to understand how characters are stored. In ASCII, characters like '0'
, '1'
, ..., '9'
are stored with specific decimal values:
'0'
is 48 in decimal.'1'
is 49 in decimal.'7'
is 55 in decimal.So, if we have a character like '7'
, it is stored as 55 in decimal (or 0011 0111 in binary).
To convert this character to the integer value 7
, we can use the following approach:
'0'
from the ASCII value of the character.'0'
to '9'
.To convert the character '7'
to its integer value:
'7'
, which is 55.Subtract the ASCII value of '0'
(which is 48) from 55:
55 - 48 = 7
7
, is the integer representation of the character '7'
.char digitChar = '7';
int digitInt = digitChar - '0';
Here, digitInt
will hold the integer value 7
.
The ASCII values for the characters '0'
to '9'
are in a contiguous range (48 to 57). Subtracting '0'
from any digit character aligns it with its corresponding integer value:
'1' - '0'
results in 1'2' - '0'
results in 2'9' - '0'
, which results in 9Well now, if we have string something like 123
, how to convert it to integer.
C
's standard library provides a function to do so, which is atoi
(ASCII to integer). It converts a string to an integer. It is defined in stdlib.h
header. It is commonly used to convert string representing numbers, like "123"
, into acutal integers (123
).
result
, initialized to 0
. This variable will store the final integer value.result
by shifting result
left by one decimal place (multiplying by 10).c
in the string:c
to its integer value by subtracting '0'
.result
by 10
and add the integer value of c
.result
left by one decimal place, making space for the new digit."123"
to Integer:Let's go through an example with the string "123"
:
result = 0
.'1'
:'1'
to integer: 1 - '0' = 1
.result
: result = result * 10 + 1 = 0 * 10 + 1 = 1
.'2'
:'2'
to integer: 2 - '0' = 2
.result
: result = result * 10 + 2 = 1 * 10 + 2 = 12
.'3'
:'3'
to integer: 3 - '0' = 3
.result
: result = result * 10 + 3 = 12 * 10 + 3 = 123
.At the end of this process, result
holds the integer value 123
.
#include <stdio.h>
int stringToInt(const char* str) {
int result = 0;
// Loop through each character in the string until the null terminator
for (int i = 0; str[i] != '\0'; i++) {
// Convert the character to integer and accumulate in result
result = result * 10 + (str[i] - '0');
}
return result;
}
int main() {
const char* str = "123";
int number = stringToInt(str);
printf("The integer value is: %d\n", number);
return 0;
}
for
loop iterates through each character in the string str
until it encounters the null terminator '\0'
, which marks the end of the string.'0'
to get its integer equivalent, then update result
by shifting it left by one decimal place (multiplying by 10) and adding the new digit.'-'
character and adjust the logic accordingly.'0'
and '9'
.In many programming languages, there are standard functions for converting strings to integers:
atoi()
functionstd::stoi()
in <string>
headerint()
functionInteger.parseInt()
These functions handle additional cases like negative numbers, but understanding the manual conversion process is helpful for learning how numbers are represented and manipulated in programming.
And yet you incessantly stand on their slates, when the White Rabbit: it was YOUR table,' said.
Learn efficient ways to append characters to C++ strings using push_back, +=, append, and +. Compare time complexity, performance, and memory usage for optimal string manipulation.
Localhost refers to the local computer, mapped to IP `127.0.0.1`. It is essential for development, allowing testing and debugging services on the same machine. This article explains its role, shows how to modify the hosts file in Linux and Windows.