Beruflich Dokumente
Kultur Dokumente
A STRING is a null terminated (\0 sentinel string character ) array of characters A new data type is now considered, namely, the character string, which is used to represent a sequence of characters regarded as a single data item. In C++ strings of characters are held as an array of characters, one character held in each array element. In addition a special null character, represented by `\0', is appended to the end of the string to indicate the end of the string. strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL ( )character: Header file : <string.h>
NOTE: although both "phrase" and "list" are arrays of characters, only "phrase" is big enough to contain the string value ""Enter age: "". We don't care what characters are stored in the variables "phrase[12]" and "phrase[13]", because all the string functions introduced below ignore characters after the "'\0'".
A string variable s1 could be declared as follows: char s1[10]; The string variable s1 could hold strings of length up to nine characters since space is needed for the final null character. Strings can be initialised at the time of declaration just as other variables are initialised. For example: char s1[] = "example"; char s2[20] = "another example" would store the two strings as follows: s1 |e|x|a|m|p|l|e|\0| s2 |a|n|o|t|h|e|r| |e|x|a|m|p|l|e|\0|?|?|?|?|
NOTE:In the first case the array would be allocated space for eight characters, that is space for the seven characters of the string and the null character. In the second case the string is set by the declaration to be twenty characters long but only sixteen of these characters are set, i.e. the fifteen characters of the string and the null character. Note that the length of a string does not include the terminating null character.
puts
output used for the strings/special output for
string syntax: puts(<variable>);
gets
input used for the strings/special input for strings
syntax: gets(<variable>);
cout
output used for the strings/special output for
string syntax: cout<<string;
cin.getline
input used for the strings/special input for strings
10
11
12
14
15
16
17
20
21
str.insert(4, I know);
22
str += correct;
23
24
strcpy( )
- used to copy the content of string2 into string1 Syntax: strcpy(string1,string2)
main()
{ char string1[8]; strcpy(string1,hello); . . }
strcat( )
- concatenates a copy of string2 to string1 with a null. The string is untouched by the operation Syntax: strcat(string1,string2) Example: this program appends the first string inputted to the second string inputted. For example assuming the user enters hello and there, the program will pring hellothere.
main() {
char string1[8],string2[8];
gets(string1); gets(string2); strcat(string1,string2); puts(string1);
.
. }
strcmp( )
- lexicographically compares two null terminated strings.
Syntax: (strcmp(string1,string2)(<,>,= =)0) Related Function: strcmpi(),stricmp(),strcoll() Value <0 Meaning string1 is less than string2
>0
= =0
strcspn( )
- returns the index or the subscript of the first character in the string pointed to by string1 that matches any of the characters in the string pointed to by string2. Syntax: strcspn(string1,string2)
strlen( )
Syntax: strlen(string1)
Example: This program prints the number 4 if the entered value of string1 isRHEN. main() { char string1[20]; gets(string1); printf(%i,strlen(string1));}
strlwr( )
- converts the string pointed to by string1 to lowercase
strncpy( )
- used to copy up to count characters from the string pointed to by source into the string pointed to by destination, the source must be a pointer to a null terminated string. Syntax: strncpy(string1,string2)
Example: The following code fragment copies at most 79 characters of string1 into string2, thus ensuring that no array boundaries overflow will occur.
Main() { char string1[120],string2[80]; gets(string1); strncpy(string2,string1,79);}
strrev( )
- reverses all the characters, except the null terminator, in the string pointed to by str. It returns str. Syntax: strrev(string) Example: This programs prints hello backwards on the screen.
Main() { char s[ ]=hello; strrev(s); printf(s); return 0; }
35
37
38
42
Summary
String literal: any sequence of characters enclosed in double quotation marks string class can be used to construct a string string class objects are used when comparing, searching, examining, or extracting a substring, or replacing, inserting, or deleting characters cin object terminates input when a blank is encountered
C++ for Engineers and Scientists, Second Edition 43
Summary (continued)
getline() method can be used for string class data input cout object can be used to display string class strings
44