PIC10A Lecture 16

Reading

p351-360, 372-375

Functions in <cstring>

strlen strcpy and strncpy
"=" can't be used in string assignment
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[6] = "Hello";
char b[6];
b = a; // compiling error cout << a << endl; cout << b << endl; return 0; }

Use strcpy to copy strings.
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[6] = "Hello";
char b[6];
strcpy(b,a); // copy the a to b cout << a << endl; cout << b << endl; return 0; }

Another example of copying string.
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[10];
strcpy(a, "PIC10A"); cout << a << endl; return 0; }

strcpy does not check to make sure the array is large enough to hold all the characters.
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[3]; strcpy(a, "PIC10A"); // not enough space, "PIC10A" has 6 characters cout << a << endl; return 0; }

Use strncpy to make sure that the string is big enough for all the characters.
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[] = "Hello"; // 5 characters
strncpy(a, "Absolute C++ is great!", 5); // don't copy more than 5 characters cout << a << endl; return 0; }

Output, only 5 characters are copied.
Absol
strcat, strncat

Example
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char greeting[] = "Hello! ";
char name[] = "Charles";
char sentence[30];

strcpy(sentence, greeting);// copy greeting to sentence
strcat(sentence, name); // append name to sentence cout << sentence << endl; return 0; }

Output
Hello! Charles

strcmp, strncmp
Example
#include<iostream>
#include<cstring>
using namespace std;

int main() {
char a[80], b[80];
cout << "Please input 2 words\n";
cout << "First word: ";
cin >> a; cout << "Second word: "; cin >> b; if(strcmp(a,b) == 0) {
cout << "They are equal.\n";
} else {
cout << "They are not equal.\n";
}
return 0;
}

Output 1
Please input 2 words
First word: UCLA Second word: UCLA They are equal.

Output 2
Please input 2 words
First word: PIC10A Second word: PIC10B They are not equal.

C-String input


A simple input
#include<iostream>
using namespace std;

int main() {
char name[80];
cout << "Please enter your name: ";
cin >> name; cout << "Hello! " << name << endl;; return 0; }

Output1
Please enter your name: Amy
Hello! Amy
      

Output2 : the input only read up to the space. Only first name is showed.
Please enter your name: Charles Li
Hello! Charles
      

Use getline to input a whole line.
#include<iostream>
using namespace std;

int main() {
char name[20];
cout << "Please enter your name: ";
cin.getline(name, 20);
cout << "Hello! " << name << endl;;

return 0;
}

Output
Please enter your name: Charles Li
Hello! Charles Li
      

If you enter more than 19 characters, only the first 19 characters is stored.
Please enter your name: abcdefghijklmnopqrstuvwxyz
Hello! abcdefghijklmnopqrs
      

Character-manipulating functions


Example
#include<iostream>
#include<cstring>
#include<cctype> using namespace std; // return true if s is in number format bool isNumber(char s[]); int main() { char input[80]; cout << "Your input: "; cin.getline(input, 80); if(isNumber(input)) { cout << "The input is a number.\n"; } else { cout << "The input is not a number.\n"; } return 0; } bool isNumber(char s[]) { for(int i = 0; i < strlen(s); i++) {
if(!isdigit(s[i])) { // if one character is not a digit
return false;
}
}
return true; // all of the characters are digits
}

Output1
Your input: 314159
The input is a number.
      

Output2
Your input: 3445ab33
The input is not a number.