#include <iostream>#include <cstdlib>#include <cstring>usingnamespace std;
void testCompare(constchar* s1, constchar* s2);
int main()
{
constchar *s; /// pointer to char
s = "Testing"; // String literals are not mutable// The const means that the array that s points to// cannot be modified.
cout << s <<"\n";
// s itself can be modified to point to a different string
s = "Different string";
cout << s <<"\n";
// Can instead declare an array that's initialized with characterschar m[100] = "This is a test";
cout << m <<"\n";
m[4] = 'Z';
cout << m <<"\n";
// Now the contents of m can be updated, but not m itself.//m = "Different string"; // error
m[0] = 'D';
m[1] = 'i';
m[2] = 'f';
m[3] = '\0';
cout << m <<"\n";
// strlen -- length up to but not including null.
cout << strlen(m) <<"\n"; // 3
cout << strlen(s) <<"\n"; // 16// String comparisons -- strcmp returns an integer// == 0 strings are equal// < 0 first string comes first in alphabet// > 0 first string comes second in alphabet
testCompare("apple", "bee"); // -1
testCompare("bee", "apple"); // 1
testCompare("bee", "bee"); // 0
testCompare("bee", "grape"); // -5 because 'b'-'g'
testCompare("grape", "bee"); // +5 because 'g'-'b'
testCompare("apple", "apply"); // -20 because 'e'-'y'
testCompare("to", "too"); // -111, because '\0' - 'o'
testCompare("Zebra", "apple"); // -7 because not case sensitive
testCompare("Zebra", "Apple"); // +25 because 'Z'-'A'// There is NOT a version that is case-insensitive,// apparently I fantasized that.//cout << "stricmp(Zebra, apple) --> "// << strcmpi("Zebra", "apple") << "\n";// Joining two strings together -- strcatchar s1[100] = "Hello";
char s2[] = "world";
strcat(s1, s2); // append characters from s2 onto end of s1// BIG DANGEROUS CAVEAT: s1 must already be a big enough array// to store both strings.
cout << s1 <<"\n";
return0;
}
void testCompare(constchar* s1, constchar* s2)
{
cout <<"strcmp("<< s1 <<", "<< s2 <<") --> "<< strcmp(s1, s2) <<"\n";
}
#include <iostream>#include <string>#include <cstring>usingnamespace std;
int main()
{
string s = "Hello";
string t = "world";
string k = s + t;
cout << k << "\n";
cout << k.size() << "\n"; // number of characters
cout << k.find('o') << "\n"; // first o at position 4
cout << k.find('o', 5) << "\n"; // next o is position 6if(k.find('z') == -1) {
cout << "Did not find z\n";
}
cout << k.substr(3,5) << "\n";
constchar* p = k.c_str(); // convert to C string
cout << strlen(p) << "\n";
char q[] = "Testing this string";
cout << string(q).substr(0,7) << "\n"; // convert to C++ stringreturn0;
}
#include <iostream>usingnamespace std;
struct card { // declares a new typeint rank;
int suit;
};
// Arrays are a composite type where each element has the SAME type// and you distinguish them by index.// A struct is a composite type where each element type CAN be// different, and you distinguish them by name.void print_card(card c) {
switch(c.rank) {
case1: cout << "Ace"; break;
case11: cout << "Jack"; break;
case12: cout << "Queen"; break;
case13: cout << "King"; break;
default: cout << c.rank;
}
cout << " of ";
switch(c.suit) {
case0: cout << "Spades"; break;
case1: cout << "Diamonds"; break;
case2: cout << "Hearts"; break;
case3: cout << "Clubs"; break;
}
cout << "\n";
}
int main()
{
card deck[52];
int i = 0;
for(int s = 0; s < 4; s++) {
for(int r = 1; r <= 13; r++) {
deck[i].rank = r;
deck[i].suit = s;
print_card(deck[i]);
i++;
}
}
}