PIC10A Lecture 15

String

C-String

We can use an array of chars to represent a string.
Example, my name Charles can be represented by:
char name[] = {'C', 'h', 'a', 'r', 'l', 'e', 's'};
Not quite!
char name[] = {'C', 'h', 'a', 'r', 'l', 'e', 's', '\0'};
A string stored in this way is called C-string.
It is very inconvenient to initialize a string in the way shown above.
C++ allows us to initialize a C-String like this :
char name[] = "Charles";
This means
name[0] name[1] name[2] name[3] name[4] name[5] name[6] name[7]
C h a r l e s \0
You can also specific the size of an array in a C-String declaration. Example :
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 ? ?
What's wrong about the following program?
#include<iostream>
using namespace std;

int main() {
char a[3] = "abc";
return 0;
}

Copy a string is not as easy as you think. There will be a compiling error in the following program
#include<iostream>
using namespace std;

int main() {
char name[8];
name = "charles"; // you can't assign string
// instead, use strcpy function
return 0;
}

cout a CString

Example
#include<iostream>
using namespace std;

int main() {
char greeting[] = "Hello!"; char name[] = "Charles"; cout << greeting; // Hello!
cout << " "; // a space
cout << name; // Charles
cout << endl;

return 0;
}
Output
Hello! Charles

Compare to the following program.
#include<iostream>
using namespace std;

int main() {
int a[] = {1,2,3};
cout << a << endl;
return 0; }
Something meaningless is printed(actually the address of a is printed).
0012FF74

Also compare to the following program
#include<iostream>
using namespace std;

int 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; return 0; }
Output
Amy
Amy#Amy
Recall:
A C-string is an array of chars with a null character '\0' at the end.

C-string in function

Question: Write a function int length(char s[]). It returns the number of charaters of a C-string.
Answer
#include<iostream>
using namespace std;

int length(char s[]); int main() { char greeting[] = "Hi PIC10A!!!"; cout << "The length of the string is: " << length(greeting) << endl; return 0; } int length(char s[]) { int index = 0; while(s[index] != '\0') { // when the charater is not null index++; } return index; }
Output
The length of the string is: 12
Keypoint
Use the following while-loop to go through every element in the string.
    while(s[index] != '\0') { 
// do something to s[index]
// your codes

index++;
}

One more question: Write a function to change all 'a' in a string to 'z'.
#include<iostream>
using namespace std;

void aToZ(char s[]); int main() { char string[] = "Pic10a 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; return 0; } 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++; } }
Output
Before the function call, string is
Pic10a students, how are you?
After the function call, string is
Pic10z students, how zre you?

Useful functions

Example, the program about length function can be rewritten as
#include<iostream>
#include<cstring> using namespace std; int main() { char greeting[] = "Hi PIC10A!!!"; cout << "The length of the string is: " << strlen(greeting) << endl; return 0; }