String and Cstring Comparisons
Topics |
string |
cstring |
Declaration and storage |
string word; |
char word[12]; |
|
string line; |
char line[81]; |
|
string sentence, paragraph; |
char sentence[101], paragraph[501]; |
Assignment |
line = "Hello Dolly!"; |
strcpy(line, "Hello Dolly!"); |
|
sentence = line; |
strcpy(sentence, line); |
Calculating Length |
len = line.length( ); |
len = strlen(line); |
|
len = string("Jerry Hultin").length( ); |
len = strlen("Jerry Hultin"); |
Input and Output |
cin >> word; |
cin >> word; |
|
cout << word << '\n' << sentence; |
cout << word << '\n' << sentence; |
|
getline(cin, line); |
cin.getline(line, 80); |
|
getline(cin, paragraph, '#'); |
cin.getline(paragraph, 500, '#'); |
String Comparison |
if (word > "apple") ... |
if (strcmp(word, "apple") > 0) ... |
|
if (line == sentence) ... |
if (strcmp(line, sentence) == 0) ... |
|
if ("" < line) ... |
if (strcmp("", line) < 0) ... |
String Concatenation |
word = word + ", "; |
strcat(word, ", "); |
|
line += word; |
strcat(line, word); |
As Function Parameters |
void encode(string& line); |
void encode(char line[]); |
|
int numberSpaces(const string& line); |
int numberSpaces(const char line[]); |
|
string upperCase(const string& line); |
(Requires dynamic memory allocation) |