chars to represent a string.
char name[] = {'C', 'h', 'a', 'r', 'l', 'e', 's'};
Not quite!'\0' at the end of a
string.
char name[] = {'C', 'h', 'a', 'r', 'l', 'e', 's', '\0'};
|
char name[] = "Charles"; |
| name[0] | name[1] | name[2] | name[3] | name[4] | name[5] | name[6] | name[7] |
| C | h | a | r | l | e | s | \0 |
char name[10] = "Hi Mom!"; |
| name[0] | name[1] | name[2] | name[3] | name[4] | name[5] | name[6] | name[7] | name[8] | name[9] |
| H | i | M | o | m | ! | \0 | ? | ? |
#include<iostream>
using namespace std;
void main() {
char a[3] = "abc"; // incorrect, compiling error, but a[3] should be a[4]
char line[100];
cin.getline(line,100);
}
|
#include<iostream>
using namespace std;
void main() {
char name[8];
name = "charles"; // you can't assign string to an array of character, compiling error
// instead, use strcpy function
char line[100];
cin.getline(line,100);
}
|
cout a cstring, every single element
before '\0' will be printed out
#include<iostream>
using namespace std;
void main() {
char greeting[] = "Hello!";
char name[] = "Charles";
cout << greeting; // Hello!
cout << " "; // a space
cout << name; // Charles
cout << endl;
char line[100];
cin.getline(line,100);
}
|
Hello! Charles |
#include<iostream>
using namespace std;
void main() {
int a[] = {1,2,3};
cout << a << endl;
char line[100];
cin.getline(line,100);
}
|
a is printed).
0012FF74 |
#include<iostream>
using namespace std;
void main() {
char name[] = {'A', 'm', 'y', '\0'}; // a C-string
char name2[] = {'A', 'm', 'y'}; // not a C-string
cout << name; // normal output
cout << endl;
cout << name2; // strange output
cout << endl;
char line[100];
cin.getline(line,100);
}
|
Amy Amy#Amy |
chars with a null
character '\0' at the end. chars.int length(char s[]). It
returns the number of charaters of a C-string.
#include<iostream>
using namespace std;
int length(char s[]);
void main() {
char greeting[] = "Hi! Math2220 students!!!";
cout << "The length of the string is: "
<< length(greeting)
<< endl;
char line[100];
cin.getline(line,100);
}
int length(char s[]) {
int index = 0;
while(s[index] != '\0') { // when the charater is not null
index++;
}
return index;
}
|
The length of the string is: 24 |
while(s[index] != '\0') {
// do something to s[index]
// your codes
index++;
}
|
#include<iostream>
using namespace std;
void aToZ(char s[]);
void main() {
char string[] = "Math2220 students, how are you?";
cout << "Before the function call, string is \n"
<< string << endl;
aToZ(string);
cout << "After the function call, string is \n"
<< string << endl;
char line[100];
cin.getline(line,100);
}
void aToZ(char s[]) {
int index = 0;
while(s[index] != '\0') { // when the charater is not null
// do something to s[index]
if(s[index] == 'a')
s[index] = 'z';
index++;
}
}
|
Before the function call, string is Math2220 students, how are you? After the function call, string is Mzth2220 students, how zre you? |
#include<cstring>strcpy, strncpy: string copy.strcat, strncat: append one string to the
other.strlen: length of the stringstrcmp, strncmp : compare string.length function can be
rewritten as
#include<iostream>
#include<cstring>
using namespace std;
void main() {
char greeting[] = "Hi! Math2220 students!!!";
cout << "The length of the string is: "
<< strlen(greeting)
<< endl;
char line[100];
cin.getline(line,100);
}
|
The length of the string is: 24 |