Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE202: Lecture 17The Ohio State University1 C Strings.

Similar presentations


Presentation on theme: "CSE202: Lecture 17The Ohio State University1 C Strings."— Presentation transcript:

1 CSE202: Lecture 17The Ohio State University1 C Strings

2 CSE202: Lecture 17The Ohio State University2 Types of Strings 1.String Literals: “Hello World” “xyz 123 *&^#$!” 2.C-style strings: char s[20]; 3.C++ class string; string s;

3 CSE202: Lecture 17The Ohio State University3 literalString.cpp... int main() { char s[] = "Hello World"; cout << s << endl; for (int i = 0; i < 11; i++) { cout << s[i] << ","; } cout << endl;...

4 CSE202: Lecture 17The Ohio State University4 literalString.cpp... char s[] = "Hello World"; cout << s << endl; for (int i = 0; i < 11; i++) { cout << s[i] << ","; } cout << endl;... > literalString.exe Hello World H,e,l,l,o,,W,o,r,l,d,

5 CSE202: Lecture 17The Ohio State University5 Literal String To create a variable containing a literal string: char s[] = “Hello World”; char s[] means an array of characters; This variable cannot be changed, i.e., the following will generate a syntax error: char s[] = “Hello World”; s = “Goodbye World”; // Syntax Error

6 CSE202: Lecture 17The Ohio State University6 ascii.cpp... int main() { char s[] = "Hello World"; cout << s << endl; for (int i = 0; i < 11; i++) { cout << setw(4) << s[i] << ","; } cout << endl; for (int i = 0; i < 11; i++) { cout << setw(4) << int(s[i]) << ","; } cout << endl; return 0; }

7 CSE202: Lecture 17The Ohio State University7 > ascii.exe Hello World H, e, l, l, o,, W, o, r, l, d, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, >... char s[] = "Hello World"; cout << s << endl; for (int i = 0; i < 11; i++) { cout << setw(4) << s[i] << ","; } cout << endl; for (int i = 0; i < 11; i++) { cout << setw(4) << int(s[i]) << ","; } cout << endl;...

8 CSE202: Lecture 17The Ohio State University8 null Character Literal strings end in a null character: ‘\0’. (Character ‘\0’ has ASCII code 0.) char s[] = “Hello World”; – s[0] equals ‘H’ ; – s[1] equals ‘e’ ;... – s[8] equals ‘r’ ; – s[9] equals ‘l’ ; – s[10] equals ‘d’ ; – s[11] equals ‘\0’ ;

9 CSE202: Lecture 17The Ohio State University9 literalString2.cpp... int i; char s[] = "Hello World"; cout << s << endl; i = 0; while(s[i] != '\0') { cout << s[i] << ","; i++; } cout << endl; cout << "s[" << i << "] = " << int(s[i]) << endl;...

10 CSE202: Lecture 17The Ohio State University10 literalString2.cpp cout << s << endl; i = 0; while(s[i] != '\0') { cout << s[i] << ","; i++; } cout << endl; cout << "s[" << i << "] = " << int(s[i]) << endl; > literalString2.exe Hello World H,e,l,l,o,,W,o,r,l,d, s[11] = 0

11 CSE202: Lecture 17The Ohio State University11 ascii2.cpp... i = 0; while(s[i] != '\0') { cout << setw(4) << s[i] << ","; i++; } cout << endl; i = 0; while(s[i] != '\0') { cout << setw(4) << int(s[i]) << ","; i++; } cout << setw(4) << int(s[i]); cout << endl;...

12 CSE202: Lecture 17The Ohio State University12 > ascii2.exe Hello World H, e, l, l, o,, W, o, r, l, d, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0 >... i = 0; while(s[i] != '\0') { cout << setw(4) << int(s[i]) << ","; i++; } cout << setw(4) << int(s[i]); cout << endl;...

13 CSE202: Lecture 17The Ohio State University13 C-style strings A C-style string is stored in an array of char; C-style strings should always end in ‘\0’; const int MAX_LENGTH(20); char s[MAX_LENGTH] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}; cout << "String = " << s << endl;

14 CSE202: Lecture 17The Ohio State University14 cString.cpp const int MAX_LENGTH(20); // C-style strings should always end in '\0' char s[MAX_LENGTH] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}; cout << "String = " << s << endl; s[1] = 'o'; s[2] = 'w'; s[3] = 'd'; s[4] = 'y'; cout << "String = " << s << endl; s[5] = '\0'; cout << "String = " << s << endl;

15 CSE202: Lecture 17The Ohio State University15 cString.cpp const int MAX_LENGTH(20); // C-style strings should always end in '\0' char s[MAX_LENGTH] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0'}; cout << "String = " << s << endl; s[1] = 'o'; s[2] = 'w'; s[3] = 'd'; s[4] = 'y'; cout << "String = " << s << endl; s[5] = '\0'; cout << "String = " << s << endl; > cString.exe String = Hello World String = Howdy World String = Howdy

16 CSE202: Lecture 17The Ohio State University16 Literal String A literal string variable cannot be changed, i.e., the following will generate a syntax error: char s[] = “Hello World”; s = “Goodbye World”; // Syntax Error However, individual characters within the string can be changed: s[1] = 'o'; s[2] = 'w'; A literal string should ALWAYS contain ‘\0’.

17 CSE202: Lecture 17The Ohio State University17 Capitalize Change all characters in a string to capitals. Input: Hello World Output: HELLO WORLD

18 ASCII Code CSE202: Lecture 17The Ohio State University18 CodeChar 32Space 33! 34" 35# 36$ 37% 38& 39' 40( 41) …… CodeChar 480 491 502 513 524 535 546 557 568 579 …… CodeChar 65A 66B 67C 68D 69E 70F 71G 72H 73I 74J …… CodeChar 97a 98b 99c 100d 101e 102f 103g 104h 105i 106j ……

19 CSE202: Lecture 17The Ohio State University19 capitalize.cpp... char s[] = "Hello World"; int ascii_code(0); int i(0); cout << s << endl; i = 0; while(s[i] != '\0') { ascii_code = int(s[i]); if (97 <= ascii_code && ascii_code <= 122) { ascii_code = ascii_code-32; } cout << char(ascii_code); i++; } cout << endl;...

20 CSE202: Lecture 17The Ohio State University20 > capitalize.exe Hello World HELLO WORLD … i = 0; while(s[i] != '\0') { ascii_code = int(s[i]); if (97 <= ascii_code && ascii_code <= 122) { ascii_code = ascii_code-32; } cout << char(ascii_code); i++; } …

21 CSE202: Lecture 17The Ohio State University21 passLiteral.cpp... int main() { int x1, y1; int x2, y2; read_point("Enter first point: ", x1, y1); read_point("Enter second point: ", x2, y2); write_point("First point: ", x1, y1); write_point("Second point: ", x2, y2); return 0; }...

22 CSE202: Lecture 17The Ohio State University22 passLiteral.cpp... void read_point(const char prompt[], int & x, int & y); void write_point(const char label[], int x, int y); int main() { int x1, y1; int x2, y2; read_point("Enter first point: ", x1, y1); read_point("Enter second point: ", x2, y2); write_point("First point: ", x1, y1); write_point("Second point: ", x2, y2); return 0; }...

23 CSE202: Lecture 17The Ohio State University23 Functions read_point() and write_point() void read_point(const char prompt[], int & x, int & y) { cout << prompt; cin >> x; cin >> y; } void write_point(const char label[], int x, int y) { cout << label; cout << "(" << x << "," << y << ")" << endl; }

24 CSE202: Lecture 17The Ohio State University24 > passLiteral.exe Enter first point: 10 20 Enter second point: 3 5 First point: (10,20) Second point: (3,5) … read_point("Enter first point: ", x1, y1); read_point("Enter second point: ", x2, y2); … void read_point(const char prompt[], int & x, int & y) { cout << prompt; cin >> x; cin >> y; }

25 CSE202: Lecture 17The Ohio State University25 > passLiteral.exe Enter first point: 10 20 Enter second point: 3 5 First point: (10,20) Second point: (3,5) … write_point("First point: ", x1, y1); write_point("Second point: ", x2, y2); … void write_point(const char label[], int x, int y) { cout << label; cout << "(" << x << "," << y << ")" << endl; }

26 CSE202: Lecture 17The Ohio State University26 (Major) Problems with C-style Strings const int MAX_LENGTH(20); char s[MAX_LENGTH] = { 'H', 'e', 'l', 'l', 'o', '\0'}; cout << "String = " << s << endl; Forgetting to end the string with ‘\0’; Not allocating enough memory for the string; How do you add an element to a string? How do you delete an element from a string? How do you concatenate two strings?

27 CSE202: Lecture 17The Ohio State University27 C++ Strings

28 CSE202: Lecture 17The Ohio State University28 C++ class string C++ class string requires: #include using namespace std; To create a variable of type string, simply: string lastName; Assignment, as always is the same: lastName = “Marx”; Or combine the two with an initialization: string lastName(“Marx”); Or string lastname = “Marx”;

29 CSE202: Lecture 17The Ohio State University29 String Operators: Assignment Assignment (=): As with before, assign a string to a variable of type string. string lastName(“Marx”), anothername; anothername = lastName; Both now hold “Marx”

30 CSE202: Lecture 17The Ohio State University30 String Operators: Concatentation Concatenation (+): Puts a string on the end of another. string firstName(“Groucho”); string lastName(“Marx”); string fullname = firstName + lastname;

31 CSE202: Lecture 17The Ohio State University31 concatString.cpp #include #include // <--------- Note using namespace std; int main() { string name1("Groucho“), name2(“Harpo”); string lastName("Marx“); string fullName1 = name1 + lastName; string fullName2 = name2 + lastName; cout << fullName1 << endl; cout << fullName2 << endl;

32 CSE202: Lecture 17The Ohio State University32 concatString.cpp #include #include // <--------- Note... { string name1("Groucho“), name2(“Harpo”); string lastName("Marx“); string fullName1 = name1 + lastName; string fullName2 = name2 + lastName; cout << fullName1 << endl; cout << fullName2 << endl; > concatString.exe GrouchoMarx HarpoMarx

33 CSE202: Lecture 17The Ohio State University33 concatString2.cpp #include #include // <--------- Note using namespace std; int main() { string name1("Groucho“), name2(“Harpo”); string lastName("Marx“); // separate first and last names with a blank string fullName1 = name1 + " " + lastName; string fullName2 = name2 + " " + lastName; cout << fullName1 << endl; cout << fullName2 << endl;

34 CSE202: Lecture 17The Ohio State University34 concatString2.cpp #include #include // <--------- Note... { string name1("Groucho“), name2(“Harpo”); string lastName("Marx“); // separate first and last names with a blank string fullName1 = name1 + " " + lastName; string fullName2 = name2 + " " + lastName; cout << fullName1 << endl; cout << fullName2 << endl; > concatString.exe Groucho Marx Harpo Marx

35 CSE202: Lecture 17The Ohio State University35 C++ String I/O

36 CSE202: Lecture 17The Ohio State University36 Input/Output with Strings I/O with Strings are as before: string lastName; cout << “Please enter your last name: “; cin >> lastName;// get the last name cout << “Your last name is “ << lastName;

37 CSE202: Lecture 17The Ohio State University37 getName.cpp #include using namespace std; int main() { string firstName, lastName, fullName; cout << "Enter your first name: "; cin >> firstName; cout << "Enter your last name: "; cin >> lastName; // concatenate fullName = firstName + " " + lastName; cout << "Your name is: " << fullName << endl;

38 CSE202: Lecture 17The Ohio State University38 getName.cpp string firstName, lastName, fullName; cout << "Enter your first name: "; cin >> firstName; cout << "Enter your last name: "; cin >> lastName; // concatenate fullName = firstName + " " + lastName; cout << "Your name is: " << fullName << endl; > getName.exe Enter your first name: Groucho Enter your last name: Marx Your name is: Groucho Marx

39 CSE202: Lecture 17The Ohio State University39 getName2.cpp #include using namespace std; int main() { string fullName; cout << "Enter your full name: "; cin >> fullName; cout << "Your name is: " << fullName << endl; return 0; }

40 CSE202: Lecture 17The Ohio State University40 getName2.cpp... string fullName; cout << "Enter your full name: "; cin >> fullName; cout << "Your name is: " << fullName << endl;... > getName2.exe Enter your full name: Groucho Marx Your name is: Groucho

41 CSE202: Lecture 17The Ohio State University41 Input/Output with Strings A common problem with reading strings from user input is that it could contain white spaces. cin uses white space (e.g. space, tab, newline) as a delimiter between inputs; cin >> fullName; > getName2.exe Enter your full name: Groucho Marx Your name is: Groucho

42 CSE202: Lecture 17The Ohio State University42 String I/O: getline() Fortunately, the string class let’s us get around this with the getline() function. Syntax: getline(source, destination) source is the source of the string. In our case, we want cin here. destination is the string variable where we want the string to be read into.

43 CSE202: Lecture 17The Ohio State University43 String I/O: getline() We can fix our code by rewriting it as follows: string fullname; cout << “Enter your full name: ”; getline(cin, fullname);

44 CSE202: Lecture 17The Ohio State University44 getName3.cpp #include using namespace std; int main() { string fullName; cout << "Enter your full name: "; getline(cin, fullName); // <--------- Note cout << "Your name is: " << fullName << endl; return 0; }

45 CSE202: Lecture 17The Ohio State University45 getName3.cpp... cout << "Enter your full name: "; getline(cin, fullName); // <--------- Note cout << "Your name is: " << fullName << endl;... > getName3.exe Enter your full name: Groucho Marx Your name is: Groucho Marx > getName3.exe Enter your full name: Groucho G. Marx III Your name is: Groucho G. Marx III

46 CSE202: Lecture 17The Ohio State University46 getName3.cpp... cout << "Enter your full name: "; getline(cin, fullName); // <--------- Note cout << "Your name is: " << fullName << endl;... > getName3.exe Enter your full name: Groucho Marx Your name is: Groucho Marx > getName3.exe Enter your full name: Your name is:

47 CSE202: Lecture 17The Ohio State University47 literalError.cpp (BAD CODE)... int main() { char firstName[10], lastName[10]; cout << "Enter first name: "; // DO NOT DO THIS. THIS IS VERY, VERY BAD! cin >> firstName; cout << "Enter last name: "; // DO NOT DO THIS. THIS IS VERY, VERY BAD! cin >> lastName; cout << "Your name is: " << firstName << " " << lastName << endl; return 0; }

48 CSE202: Lecture 17The Ohio State University48 > literalError.exe Enter first name: John Enter last name: MacGhilleseatheanaich Your name is: naich MacGhilleseatheanaich >... char firstName[10], lastName[10]; cout << "Enter first name: "; // DO NOT DO THIS. THIS IS VERY, VERY BAD! cin >> firstName; cout << "Enter last name: "; // DO NOT DO THIS. THIS IS VERY, VERY BAD! cin >> lastName; cout << "Your name is: " << firstName << " " << lastName << endl;...

49 CSE202: Lecture 17The Ohio State University49 C++ String Processing

50 CSE202: Lecture 17The Ohio State University50 str[k] s[k] represents the k’th character in string s: string s(“Hello World”); // the character ‘H’ will be output. cout << s[0] << endl; // the character ‘W’ will be output. cout << s[6] << endl;

51 CSE202: Lecture 17The Ohio State University51 String Processing (2) s[k] represents the (k+1)’st character in string s: string s(“Hello World”); s[1] = ‘o’; s[2] = ‘w’; s[3] = ‘d’; s[4] = ‘y’; // output “Howdy World” cout << s << endl;

52 CSE202: Lecture 17The Ohio State University52 Relational String Operators == and != are same as before, but the others are not exactly like usage with numbers… For instance, what exactly does it mean if one string is “less than” another? ==!= <<= >>=

53 CSE202: Lecture 17The Ohio State University53 relationOps.cpp... string s1, s2; cout << "Enter first string: "; getline(cin, s1); cout << "Enter second string: "; getline(cin, s2); if (s1 == s2) { cout << s1 << " equals " << s2 << endl; } else if (s1 < s2) { cout << s1 << " < " << s2 << endl; } else { cout " << s2 << endl; }

54 CSE202: Lecture 17The Ohio State University54 relationOps.cpp if (s1 == s2) { cout << s1 << " equals " << s2 << endl; } else if (s1 < s2) { cout << s1 << " < " << s2 << endl; } else { cout " << s2 << endl; } > relationOps.exe Enter first string: hello Enter last string: howdy hello < howdy

55 CSE202: Lecture 17The Ohio State University55 relationOps.cpp if (s1 == s2) { cout << s1 << " equals " << s2 << endl; } else if (s1 < s2) { cout << s1 << " < " << s2 << endl; } else { cout " << s2 << endl; } > relationOps.exe Enter first string: hello Enter last string: Howdy hello > Howdy

56 CSE202: Lecture 17The Ohio State University56 relationOps.cpp if (s1 == s2) { cout << s1 << " equals " << s2 << endl; } else if (s1 < s2) { cout << s1 << " < " << s2 << endl; } else { cout " << s2 << endl; } > relationOps.exe Enter first string: Hello Enter last string: Hello World (What will be the output?)

57 CSE202: Lecture 17The Ohio State University57 relationOps.cpp Blanks matter: > relationOps.exe Enter first string: HelloWorld Enter last string: Hello World HelloWorld > Hello World

58 CSE202: Lecture 17The Ohio State University58 sortNames.cpp... int main() { const int MAX_SIZE(50); string name[MAX_SIZE]; int num_names(0), i(0); string s; // Read names i = 0; cout << "Enter name: "; cin >> s; while (s != ".“ && i < MAX_SIZE) { name[i] = s; i++; cout << "Enter name: "; cin >> s; } num_names = i;...

59 CSE202: Lecture 17The Ohio State University59 sortNames.cpp (2)... // Sort names for (int i = num_names-1; i > 0; i--) { // Compare name[j] with name[i], j < i for (int j = 0; j < i; j++) { if (name[j] > name[i]) { swap(name[j], name[i]); } // name[i] is now in its correct location }...

60 CSE202: Lecture 17The Ohio State University60 sortNames.cpp (3)... // Output names cout << endl; cout << "Names in alphabetic order:" << endl; for (int i = 0; i < num_names; i++) { cout << name[i] << endl; } return 0; }

61 CSE202: Lecture 17The Ohio State University61 String Processing In addition to giving us a new data type to hold strings, the string library offers many useful string processing methods. You can find most of them of them in the book, but here are a few useful ones.

62 CSE202: Lecture 17The Ohio State University62 length() This method returns the integer length of the string. (Note: Parentheses “()” after length().) Example: string s1(“Super!”); //the integer 6 will be output. cout << s1.length() << endl;

63 CSE202: Lecture 17The Ohio State University63 easyFrench.cpp int main() { string s; cout << "Enter phrase in English: "; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i] == 's') { s[i] = 'z'; } else if (s[i] == 'w') { s[i] = 'v'; } cout << "French version: "; cout << s << endl;...

64 CSE202: Lecture 17The Ohio State University64 substr(index, n) Returns the string consisting of n characters starting from the specified index. Example: string transport(“Cruise Ship”); //outputs “Ship” cout << transport.substr(7, 4) << endl;

65 CSE202: Lecture 17The Ohio State University65 replace(index, n, str) Removes n characters in the string starting from the specified index, and inserts the specified string, str, in its place. Example: string transport(“Speed Boat”); transport.replace(0, 5, “Sail”); // outputs “Sail Boat” cout << transport << endl;

66 CSE202: Lecture 17The Ohio State University66 advancedFrench.cpp // example of using C++ class string #include using namespace std; int main() { string s; cout << "Enter phrase in English: "; getline(cin, s);

67 CSE202: Lecture 17The Ohio State University67 advancedFrench.cpp... for (int i = 0; i < s.length(); i++) { if (s[i] == 's') { s[i] = 'z'; } else if (s[i] == 'w') { s[i] = 'v'; } else if (s.substr(i, 5) == " the ") { s.replace(i, 5, " la "); } cout << "French version: "; cout << s << endl; return 0; }

68 CSE202: Lecture 17The Ohio State University68 insert(index, str) Inserts the specified string at the specified index. Example: string animal(“Hippo”); animal.insert(0, “Happy ”); //outputs “Happy Hippo” cout << animal << endl;

69 CSE202: Lecture 17The Ohio State University69 outputFileName.cpp // construct an output file name from the input file name #include using namespace std; int main() { string input_filename, output_filename; int length(0); cout << "Enter input file name: "; cin >> input_filename;...

70 CSE202: Lecture 17The Ohio State University70 outputFileName.cpp... length = input_filename.length(); output_filename = input_filename; if (input_filename.substr(0,2) == "in") { output_filename.replace(0,2,"out"); } else { output_filename.insert(0, "out."); } cout << "Output file name: " << output_filename << endl; return 0; }

71 CSE202: Lecture 17The Ohio State University71 CanadianError.cpp... int main() { string s; cout << "Enter phrase in English: "; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i] == '.' || s[i] == '?') { s.replace(i, 1, ", eh?"); } cout << "Canadian version: "; cout << s << endl; return 0; }

72 CSE202: Lecture 17The Ohio State University72 Canadian.cpp... int main() { string s; cout << "Enter phrase in English: "; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i] == '.' || s[i] == '?') { s.replace(i, 1, ", eh?"); i = i + 4; // Why 4? } cout << "Canadian version: "; cout << s << endl; return 0; }

73 CSE202: Lecture 17The Ohio State University73 Literal vs. C++ strings Literal strings end in a null character: ‘\0’. (Character ‘\0’ has ASCII code 0.) char s[] = “Hello World”; – s[11] equals ‘\0’ ; C++ strings DO NOT end in a null character: ‘\0’. string s2(“Hello World”); – s2[11] DOES NOT exist!

74 CSE202: Lecture 17The Ohio State University74 Passing Strings to Functions

75 CSE202: Lecture 17The Ohio State University75 outputFileName.cpp... length = input_filename.length(); output_filename = input_filename; if (input_filename.substr(0,2) == "in") { output_filename.replace(0,2,"out"); } else { output_filename.insert(0, "out."); } cout << "Output file name: " << output_filename << endl;...

76 CSE202: Lecture 17The Ohio State University76 Function construct_output_filename() void construct_output_filename (string & in_name, string & out_name) { out_name = in_name; if (in_name.substr(0,2) == "in") { out_name.replace(0,2,"out"); } else { out_name.insert(0, "out."); }

77 CSE202: Lecture 17The Ohio State University77 outputFileName2.cpp... void construct_output_filename (string & in_name, string & out_name); int main() { string input_filename, output_filename; cout << "Enter input file name: "; cin >> input_filename; construct_output_filename(input_filename, output_filename); cout << "Output file name: " << output_filename << endl; return 0; } // Define function construct_output_filename here....

78 CSE202: Lecture 17The Ohio State University78 Pass Strings by Reference Strings are complicated objects of arbitrarily large size. Usually strings should be passed by reference (string &).

79 CSE202: Lecture 17The Ohio State University79 Function construct_output_filename() void construct_output_filename (const string & in_name, string & out_name) { out_name = in_name; if (in_name.substr(0,2) == "in") { out_name.replace(0,2,"out"); } else { out_name.insert(0, "out."); }

80 CSE202: Lecture 17The Ohio State University80 outputFileName3.cpp... void construct_output_filename (const string & in_name, string & out_name); int main() { string input_filename, output_filename; cout << "Enter input file name: "; cin >> input_filename; construct_output_filename(input_filename, output_filename); cout << "Output file name: " << output_filename << endl; return 0; } // Define function construct_output_filename here....

81 CSE202: Lecture 17The Ohio State University81 Pass Strings by Reference Strings are complicated objects of arbitrarily large size. Usually strings should be passed by reference (string &). Strings which should not be modified in the function should be passed as constants (const string &).

82 CSE202: Lecture 17The Ohio State University82 outputFileNameError.cpp... void construct_output_filename (string & in_name, string & out_name); int main() { string output_filename; construct_output_filename(“indata1.txt”, output_filename); cout << "Output file name: " << output_filename << endl; return 0; } void construct_output_filename (string & in_name, string & out_name) {... }

83 CSE202: Lecture 17The Ohio State University83 outputFileName4.cpp... void construct_output_filename (const string & in_name, string & out_name); int main() { string output_filename; construct_output_filename(“indata1.txt”, output_filename); cout << "Output file name: " << output_filename << endl; return 0; } void construct_output_filename (const string & in_name, string & out_name) {... }

84 CSE202: Lecture 17The Ohio State University84 Pass Strings by Reference Strings are complicated objects of arbitrarily large size. Usually strings should be passed by reference (string &). Strings which should not be modified in the function should be passed as constants (const string &). Literal strings may be passed by reference to constant parameters (const string &).


Download ppt "CSE202: Lecture 17The Ohio State University1 C Strings."

Similar presentations


Ads by Google