Zum Inhalt, überspringe Kopfzeile Zur Navigation, überspringe Kopfzeile
1: /** 2: * Projekt: Strings 3: * 4: * @author Stefan Jahn 5: * @version 20070316 6: * @date 16.03.2007 7: * 8: * @file main.cpp 9: */ 10: 11: #include <iostream> 12: using namespace std; 13: 14: // Bibliothek für Strings einbinden 15: #include <string> 16: 17: /** 18: * main-Funktion 19: */ 20: int main (int argc, char const *argv[]) { 21: cout << "Strings:" << endl; 22: cout << "========" << endl; 23: 24: // Variabeln 25: string a = "Dies ist"; 26: string b = ""; 27: 28: // Der Variabel c wird kein Wert/String zugewiesen. Die Bibliothek string 29: // weisst c aber automatisch einen Leerstring "" zu. Dies geschieht durch 30: // den Konstruktor der Klasse string. 31: string c; 32: 33: // Variabel ändern 34: b = "ein Test."; 35: 36: // Werte ausgeben 37: cout << "Werte:" << endl; 38: cout << "a = " << a << endl; 39: cout << "b = " << b << endl; 40: cout << "c = " << c << endl; 41: cout << endl; 42: 43: // Länge des String ermitteln 44: // Mit der Methode size() oder length() 45: cout << "Länge:" << endl; 46: cout << "a = " << a.size() << " Zeichen" << endl; 47: cout << "b = " << b.length() << " Zeichen" << endl; 48: cout << "c = " << c.size() << " Zeichen" << endl; 49: cout << endl; 50: 51: // Zeichen an einer bestimmten Position ermitteln 52: cout << "Zeichen an einer bestimmten Position:" << endl; 53: 54: // Variante 1: Wie bei den Arrays 55: // Angabe des Index: 0...Anzahl-1, keine Überprüfung auf Gültigkeit 56: cout << "a[3] = " << a[3] << endl; 57: 58: // Variante 2: Mit der Methode at() 59: // Im Gegensatz zur Variante 1 wird hier auf eine gültige Position/Index 60: // überprüft. 61: cout << "a.at(3) = " << a.at(3) << endl; 62: cout << endl; 63: 64: // Strings verketten 65: cout << "Strings verketten:" << endl; 66: c = a+" "+b; 67: cout << "c = a+\" \"+b = " << c << endl; 68: 69: return 0; 70: }
Zum Inhalt zurück, überspringe Navigation | Zum Seitenanfang