-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreatingAndInitializingStrings.cpp
59 lines (34 loc) · 2.66 KB
/
CreatingAndInitializingStrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Creating and Initializing Strings In C
// Creating a string can be done in one of two ways. The first way is to simply create an initialized array of characters populated by the characters of the string. Here’s an example of the string “Hello World” created using an initialized array:
char str[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'};
// The character array is populated by the characters of “Hello World.” There are two important things to note here:
// - A blank space is a character, so it is included by putting a space in single quotes.
// - The very last character in the array is \0 known as the null terminating character. It signifies the end of the string and must be included when creating a string as an initialized array.
// The string “Hello World” has 11 characters in it (space is a character too!) but the size of str[] is 12: 11 characters for “Hello World” plus one for the null character.
// The second way to create a string variable in C is to use what is called a string literal. This is the creation of a string by initializing the char array with the full string in double quotes like so:
char str[] = "Hello World";
// This is still an array of characters and is to be treated as such. The only benefit to creating a string in this way is that it’s easier to type and the null terminating character (‘\0’) is added implicitly. The size of this array is still 12 for the same reasons mentioned before.
// Using the printf() function to print the string is done by providing the name of the string (array), regardless of how it was created, and "%s" to signify that printf() is working with a string. Here is an example:
char str[] = "Hello World";
printf("%s", str); // This will not print a new line
printf("%s\n", str); // This will print a new line
// Note: Characters in C are case-sensitive; i.e., the characters ‘A’ and ‘a’ are treated as two different kinds of characters.
// Create the string “Computer” using an initialized array of chars. The variable should be called comp.
#include<stdio.h>
int main() {
char comp[] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\0'};
}
// Using string literals, create a string called compCode containing the string “Computer Code”.
#include<stdio.h>
int main() {
char comp[] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\0'};
char compCode[] = "Computer Code";
}
// Print the string stored in comp followed by the string stored in compCode on separate lines.
#include<stdio.h>
int main() {
char comp[] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\0'};
char compCode[] = "Computer Code";
printf("%s\n", comp);
printf("%s", compCode);
}