and =with C-strings C-strings not like other variables Cannot assign or compare: char aString[10]; aString "Hello"; /ILLEGAL! Can ONLY use"="at declaration of c-string! Must use library function for assignment: strcpy(aString,"Hello"); Built-in function (in <cstring>) Sets value of aString equal to "Hello" ◆NO checks for size! Up to programmer,just like other arrays! Copyright0 Pearson Addison-Wesley.All rights reserved. 9-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-11 = and == with C-strings ¨ C-strings not like other variables ¨ Cannot assign or compare: char aString[10]; aString = "Hello"; // ILLEGAL! ¨ Can ONLY use "=" at declaration of c-string! ¨ Must use library function for assignment: strcpy(aString, "Hello"); ¨ Built-in function (in <cstring>) ¨ Sets value of aString equal to "Hello" ¨ NO checks for size! ¨ Up to programmer, just like other arrays!
Comparing C-strings Also cannot use operator = char aString[10]"Hello"; char anotherString[10]="Goodbye"; aString =anotherString;/NOT allowed! Must use library function again: if(strcmp(aString,anotherString)) cout <"Strings NOT same."; else cout <"Strings are same."; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-12 Comparing C-strings ¨ Also cannot use operator == char aString[10] = "Hello"; char anotherString[10] = "Goodbye"; ¨aString == anotherString; // NOT allowed! ¨ Must use library function again: if (strcmp(aString, anotherString)) cout << "Strings NOT same."; else cout << "Strings are same.";
The <cstring>Library: Display 9.1 Some Predefined C-String Functions in <cstring>(1 of 2) Full of string manipulation functions Display 9.1 Some Predefined C-String Functions in <cstring> FUNCTION DESCRIPTION CAUTIONS strcpy (Target_String_Var, Copies the C-string value Does not check to make sure Src_String) Src_String into the Target_String_Var is large C-string variable enough to hold the value Target_String_Var. Src_String. strcpy(Target_String_Var, The same as the two-argument If Limit is chosen carefully,this is Src_String,Limit) strcpy except that at most safer than the two-argument Limit characters are copied. version of strcpy.Not imple- mented in all versions of C++. strcat(Target_String_Var, Concatenates the C-string value Does not check to see that Src_String) Src_String onto the end of the Target_String_Var is large C-string in the C-string variable enough to hold the result of the Target_String_Var. concatenation. (continued) Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-13 The <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (1 of 2) ¨ Full of string manipulation functions
The <cstring>Library: Display 9.1 Some Predefined C-String Functions in <cstring>(2 of 2) Display 9.1 Some Predefined C-String Functions in <cstring> FUNCTION DESCRIPTION CAUTIONS strcat(Target_String_Var, The same as the two argument If Limit is chosen carefully,this is Src_String,Limit) strcat except that at most safer than the two-argument Limit characters are appended. version of strcat.Not imple- mented in all versions of C++. strlen(Src_String) Returns an integer equal to the length of Src_String.(The null character,'\0',is not counted in the length.) strcmp(String_1,String_2) Returns 0 if String_/and If String_lequals String_2,this String_2 are the same.Returns a function returns 0,which con- value o if String_i is less than verts to false.Note that this is String_2.Returns a value o if the reverse of what you might String_Iis greater than String_2 expect it to return when the (that is,returns a nonzero value strings are equal. if String_I and String_2 are dif- ferent).The order is lexico- graphic. strcmp(String_l, The same as the two-argument If Limit is chosen carefully,this is String_2,Limit) strcat except that at most safer than the two-argument Limit characters are compared. version of strcmp.Not imple mented in all versions of C++. Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-14 The <cstring> Library: Display 9.1 Some Predefined C-String Functions in <cstring> (2 of 2)