string
// DIFFERENT STRINGS
// C-style (cahracter arrays), requires manual memory management
// example: char example_string[] = "...";
// string objects, provides number of functions, automativ memory management
// example: std:string example_string = "...";
// character functions
// isalpha(c) //true if c is a letter
// isalnum(c) //true if c is a letter or digit
// isdigit(c) //true if c is a digit
// islower(c) //true if c is lowercase letter
// isprint(c) //true if c is a printable character
// ispunct(c) //true if c is a punctuation letter
// isupper(c) //true if c is an uppercase letter
// issapce(c) //true if c is whitespace
// c-style strings
char my_name[8] = "Frank";
my_name[5] = 'y'; // handled as array
// Functions for c-style strings
// copying
// concatenation
// Comparison
// Searching
// ...
// get string length strlen(char) (datatype size_t)
// example 1
char my_name2[] = "Frank";
char full_name[] = "leer";
char last_name[] = "Stein";
cout << "your Name " << my_name2 << " has " << strlen(my_name2) << " Characters." << endl;
// example 2 - concatenation, copying
strcpy(full_name, my_name2); // copy my_name2 to full_name
strcat(full_name," "); // concatenate full_name and a space
strcat(full_name,last_name); // concatenate last_name to full_name
cout << "your full name is " << full_name << endl;
// example 3
cout << "Enter your full name: ";
char full_name2[] = "leer 2";
cin.getline(full_name2, 50); //get max linlength of 50
// example 4 - comparison
if(strcmp(my_name,my_name2)==0)
cout << my_name << " and " << my_name2 << " are the same." << endl;
// c++ strings
#include <string>
string s0;
string s1 = "Apple";
string s5 = s1; //Apple
string s6 = s1,0,3; //App
string s7 (10,'X'); //XXXXXXX
// example 1
// search
string word = " ";
cout << "Enter the word to find: ";
cin >> word;
// example 2
// Erase
string s1 = "This is a test";
s1.erase(0,5); //erase this from s1
cout << "s1 is now:" << s1 << endl; //is a test
// example 3
// Substring
cout << "\nSubstring" << "\n-----------" << endl;
s1 = "This is a test;"
cout << s1.substr(0,4) << endl; //This
cout << s1.substr(5,2) << endl; //is
cout << s1.substr(10,4) << endl; //test
// example 4
// getline
cout << "\ngetline" << "\n-------------" << endl;
string full_name = "";
cout << "Enter your full name: ";
getline(cin,full_name)
cout << "Your full name is: " << full_name << endl;