#include<iostream>
#include<cstring>
using namespace std;
int main() {
char a[15] = "ABCDE";
char b[15];
char c[15]= "123456789";
char d[] = "abcde";
strcpy(b, a);
cout << "1: " << a << endl;
cout << "2: " << b << endl;
strncpy(b, c, 5);
cout << "3: " << b << endl;
cout << "4: " << c << endl;
strncpy(c,a,2);
cout << "5: " << a << endl;
cout << "6: " << c << endl;
strcpy(c,d);
cout << "7: " << c << endl;
cout << "8: " << d << endl;
return 0;
}
|
|
#include<iostream>
#include<cstring>
using namespace std;
void check(int a);
int main() {
char a[10] = "apple", b[10] = "apply";
char c[10] = "banana", d[10] = "ban";
char e[10] = "computer", f[10] = "camp";
check(strcmp(a,b));
check(strcmp(b,c));
check(strcmp(a,a));
check(strncmp(a,b,3));
check(strcmp(e,f));
check(strncmp(f,e,2));
strcpy(b,d);
check(strcmp(b,d));
check(strcmp(f, "CAMP"));
check(strcmp("paste", "copy"));
check(strcmp("hello", "hello"));
return 0;
}
void check(int a) {
if( a == 0) {
cout << "0\n";
} else if (a > 0) {
cout << "+\n";
} else {
cout << "-\n";
}
}
|
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char name1[20], name2[20];
cout << "Input name1: ";
cin.getline(name1, 20);
cout << "Input name2: ";
cin >> name2;
cout << "Hello " << name1 << endl;
cout << "Hello " << name2 << endl;
return 0;
}
|
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main() {
char a[] = {'a', '2', '#', 'Q'};
for(int i = 0; i < 4; i++){
cout << a[i] << endl;
if(islower(a[i])) {
cout << "lower ";
}
if(isupper(a[i])) {
cout << "upper ";
}
if(isdigit(a[i])) {
cout << "digit ";
}
cout << (char)toupper(a[i])<< " ";
cout << (char)tolower(a[i])<< " ";
cout << "\n\n";
}
}
|
void switchCase(char string[]). The function
replaces all the uppercase letters (resp. lowercase letters) to uppercase letters
(resp. uppercase letters).char a[] = "aBcD123eF";
then after the function call switchCase(a),
cout << a becomes "AbCd123Ef"void countDigit(char string[]) counts the number
of digits in the CString string. void removeDigit(char string[], char newstring[])
removes all the digits from string and save the result to newstring. char a[] = "aBcD123eF", b[100]; then
after removeDigit(a,b), b becomes "aBcDeF".