Strings in C und C++
C speichert Text in char-Arrays, die mit einem unsichtbaren '\0' enden, C++ verpackt Text in std::string, das den Speicher selbst verwaltet.
Was ist Strings in C und C++?
C speichert Text in char-Arrays, die mit einem unsichtbaren '\0' enden, C++ verpackt Text in std::string, das den Speicher selbst verwaltet.
Vergleiche C-Zeichenarrays mit std::string in C++: Länge, Verbinden, Vergleichen und Suchen.
Wichtige Punkte
- Aktiviere Compiler-Warnungen und behebe sie vollständig.
- Kenne Typ und Lebensdauer jedes Werts.
- Prüfe Eingaben und Array-Grenzen explizit.
Codebeispiele in C und C++
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
/* A C string is a char array that ends with '\0'. */
char name[32] = "Ada";
printf("%s has %zu letters\n", name, strlen(name));
/* strcat appends; the array must have room for the result. */
strcat(name, " Lovelace");
printf("%s\n", name);
/* strcmp returns 0 when two strings are equal. */
printf("equal: %d\n", strcmp("cat", "cat") == 0);
/* Walk the characters until the terminating '\0'. */
char upper[32];
size_t i = 0;
for (; name[i] != '\0'; i++) {
upper[i] = (char)toupper((unsigned char)name[i]);
}
upper[i] = '\0';
printf("%s\n", upper);
/* strstr finds a substring and returns a pointer to it, or NULL. */
printf("contains \"love\": %s\n", strstr("lovelace", "love") ? "yes" : "no");
return 0;
}
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
int main() {
// std::string manages its own memory and grows as needed.
std::string name = "Ada";
std::cout << name << " has " << name.size() << " letters\n";
name += " Lovelace";
std::cout << name << '\n';
// == compares the contents of two strings.
std::cout << "equal: " << (std::string("cat") == "cat") << '\n';
std::string upper = name;
std::transform(upper.begin(), upper.end(), upper.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); });
std::cout << upper << '\n';
// find returns std::string::npos when the text is missing.
std::string word = "lovelace";
std::cout << "contains \"love\": " << (word.find("love") != std::string::npos ? "yes" : "no") << '\n';
std::cout << "first name: " << name.substr(0, 3) << '\n';
}
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
first name: Ada
Vergleich zwischen C und C++
In C arbeiten strlen, strcat, strcmp und strstr auf rohen Arrays, und du musst selbst sicherstellen, dass das Array groß genug ist. std::string bietet size(), +=, ==, find() und substr() und wächst automatisch, was Pufferüberläufe vermeidet.
C stellt kleine prozedurale APIs und Repräsentationsdetails offen dar.
C++ behält das Low-Level-Modell und ergänzt sicherere Bibliothekstypen.
Übungsaufgaben
Führe beide Versionen aus und untersuche die unterschiedlichen Garantien.
- Füge ungültige und Grenzwerte hinzu.
- Kompiliere mit -Wall -Wextra -Wpedantic.
- Teile Deklaration und Implementierung auf.
Mit einer Challenge üben
Nutze den Inhalt dieser Lektion, um ein echtes Problem zu lösen. Jede Challenge läuft im Browser, mit sofortigen Tests und einem KI-Review.
- Count Uppercase Letters Count the capital letters in a text. Einfach
- Initialen aus einem Namen Erstelle Großbuchstaben-Initialen aus einem vollständigen Namen. Einfach
- Replace Spaces With Dashes Turn spaces into dashes to make a URL-style slug. Einfach