Math2220 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;

void main() {
    char a[3] = "abc"; // incorrect, compiling error, but a[3] should be a[4]
   
    char line[100];
    cin.getline(line,100);
}

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;

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

Example
#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);
}
Output
Hello! Charles

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

void  main() {
    int a[] = {1,2,3};
    cout << a << endl;
    
    char line[100];
    cin.getline(line,100);
}   
Something meaningless is printed(actually the address of a is printed).
0012FF74

Also compare to the following program
#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);
}
     
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[]);

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;
}

     
Output
The length of the string is: 24
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[]);

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++;
    }
}

     
Output
Before the function call, string is
Math2220 students, how are you?
After the function call, string is
Mzth2220 students, how zre you?

Useful functions

Example, the program about 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);
}
     
Output
The length of the string is: 24