Presentation is loading. Please wait.

Presentation is loading. Please wait.

cout << str1;  pear

Similar presentations


Presentation on theme: "cout << str1;  pear"— Presentation transcript:

1 cout << str1;  pear
Strings A string is an array of characters terminated by a NULL char str1[5] = {‘p’,’e’,’a’,’r’,’\0’} or equivalently char str1[5] = “pear” Input, output, and other functions will treat the array as a single string, e.g. cout << str1;  pear

2 Important Applications of Strings
Databases and configuration files Input and output functions Communication Symbolic computation Computer languages and compilers Artificial intelligence and expert systems

3 Common Control Characters
NULL \0 New line \n Horizontal tab \t Backslash \\ Double quote \” Control characters can also be placed within string constants, e.g. cout << “\nhello\ngoodbye”  hello goodbye

4 String Initialization
char str1[5] = “pear” , this statement does Defines a character array variable Allocates memory to store the array Optionally assigns an initial string value we can also define a dimensionless array char str1[] = “pear” Initialization constants cannot be used in assignments such as str1 = “pear”  invalid

5 Strings and Arrays The same caution should be used with strings as with arrays with regard to boundary and size checking Trying to access characters outside of the array’s boundaries will cause stability problems str1[ ] = ‘q’ is a headache waiting to happen Trying to store a string that is too big for an array could also cause problems

6 Common String Functions
strcpy(s1,s2) copies s2 into s1 Useful because s1 = s2 is not a valid assignment #include <string.h> // header file for string functions char s1[5], s2[5] = “pear”; strcpy(s1,s2); cout << s1;  pear

7 Common String Functions
strcat(s1,s2) concatenates s2 onto the end of s1 Useful for making a larger string from two smaller ones char s1[9]=“big ”, s2[5] = “pear”; strcat(s1,s2); cout << s1;  big pear

8 Common String Functions
strlen(s1) returns the length of s1 Useful for boundary checking Size does not include the NULL character char s1[] = “pear”; cout << strlen(s1);  4

9 Common String Functions
strcmp(s1,s2) returns 0 if s1 is equal to s2 _stricmp(s1,s2) same as above but ignores case Useful for database and lookup table functions There are many other functions listed in the VC++ help index under “string manipulation routines”

10 String Type Conversions
Sometimes it is necessary to convert from some data type such as a double to a string or vice versa A character array can be defined as an output or input stream using the ostrstream and istrstream classes These classes can be used for type conversions, e.g. #include <strstrea.h> // header file for classes char s1[8]; ostrstream sout(s1,8); sout << << '\0'; cout << s1; 

11 Multidimensional Arrays
A simple extension to 1D arrays, e.g. int i1[2][2] = {1,2,3,4}; or int i1[][2] = {1,2,3,4}; cout << i1[0][0] << “, “ << i1[0][1] ;  1,2 cout << “\n” << i1[1][0] << “, “ << i1[1][1]; 3,4 3D arrays and beyond follow the same pattern, but watch out for the memory requirements Total bytes = sizeof(int) *2*2 for the case above More will be discussed later along with pointer variables

12 Arrays of Strings Can be created by using 2D character arrays, e.g.
char s1[2][7] = {“apple”,”orange”}; cout << s1[0] << “\n”;  apple cout << s1[1]; orange The extension to 2D string arrays follows the same pattern by using 3D arrays of characters

13 Standard String Classes
Standard string classes exist that combine string storage and manipulation functions They offer no real advantages (in my humble opinion)


Download ppt "cout << str1;  pear"

Similar presentations


Ads by Google