Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.

Similar presentations


Presentation on theme: "Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld."— Presentation transcript:

1 Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld

2 Strings (arrays of characters) We have seen strings before: –When using printf, the stuff between the quotes is a string printf( “ Hi, I am a string!\n ” ); Notice that strings are represented with double quotes and are really arrays of chars (represented with single quotes) So how can we declare these strings?

3 Declaring strings char cMyFirstString[ ] = “ Hello ” ; This creates an array and initializes the elements of the array to the individual characters in the string Remember, since we don’t specify a size for the array, the initializing value determines the size Strings have a hidden character at the end that also gets added to the array… So the size of the array for “Hello” is six, not five, as the string termination character – known as a null character – is added

4 Null character Strings have a special termination character at the end that signifies the end of the string The null character is a backslash followed by a zero. That is, the character looks like: ‘\0’ All strings in C end with this character All arrays holding a string must be declared large enough to hold all the characters as well as the null character.

5 Declaring and initializing another way Remember how we initialized an integer array with initializers? int iMyFirstArray[ 5 ] = { 0, 0, 0, 0, 0 } ; Can do something similar for strings, using initializers: char cMyFirstString[] = { ‘ H ’, ‘ e ’, ‘ l ’, ‘ l ’, ‘ o ’, ‘ \0 ’ }; This creates an array and initializes the elements of the array to the individual characters (note the null character at the end)

6 Declaring and Initializing (cont’d) As with integers, if you specify fewer initializing values than you have elements, all the rest are initialized to a value of 0. For examples, char cMyFirstString[ 5 ] = { ‘ A ’ }; would set the zeroth element to ‘A’ and the rest to 0! The string would be “A”, not “A0000” –i.e. the first 0 after the character is read as the null character

7 Accessing characters in an array Since a string is an array of characters, you can access the individual elements as you would with arrays of integers. For example, with char cMyFirstString[ ] = “ Hello ” ; cMyFirstString[ 0 ] is the character ‘H’ and cMyFirstString[ 2 ] is ‘l’

8 Printing Character Arrays You can print it the hard way… for ( i = 0; sMyFirstString[ i ] != ‘ \0 ’ ; i++ ) { printf( “ %c ”, sMyFirstString[ i ] ) ; } … or an easier way: printf ( “ %s\n ”, sMyFirstString ); Note the new conversion specifier of %s for string!

9 Sample Program #1

10 Reading Input into Character Arrays You can read a string from the keyboard into a character array First, you must create an array large enough to hold your expected input char sInput[ 20 ] ; Then use scanf with the %s specifier scanf( “ %s ”, sInput ); NOTE THERE IS NO & BEFORE THE ARRAY NAME IN scanf ! –It is not needed for arrays! (Please don’t ask me why )

11 Reading Input cont’d scanf will keep filling the array until you enter a whitespace character It is up to YOU to make sure that your array is large enough for the input If the array is too small for the input, scanf will continue to write beyond the length of the array –will most likely result in run-time errors Remember, you must leave a space for the null character! If you want to enter up to 10 letters, create a string with 11 boxes.

12 12 #include #define SIZE 10 int main() { char sInput[ SIZE ]; scanf( "%s", sInput); printf("You typed %s\n", sInput); scanf( "%s", sInput); printf("You typed %s\n", sInput); return 0; } Sample Program #2

13 String (char array) functions You can use certain functions for manipulating strings by including the string.h header file –E.g. #include Useful functions include: –strcpy –strlen –strcmp

14 strcpy Allows you to copy one string, including the terminating null character, into another. E.g.: char sDestinationString[ 10 ] ; strcpy( sDestinationString, “ Class 1 ” ); Copies “Class 1”, including space, into FIRST parameter, sDestinationString No bounds checking is performed –It is up to YOU to make sure the destination string is large enough (including the null character!)

15 Modifying arrays in a function It is a peculiarity in C (don’t ask me why ) that by default you can modify the contents of an array passed to the function and its changes will be reflected as well in the block that called the function Any change you make to the elements of an array passed to the function will be propagated upwards –E.g. if we changed the array of student grades within the function, the array would be changed in main! Only works on arrays passed (not individual elements passed)

16 Some Protection C provides a mechanism with which you can make an array non-modifiable Use the keyword “const” before the declaration in your function definition and prototype. For example: –Protoype: void ArrayPrint( const int [ ], int ); –Definition void ArrayPrint( const int iArrayToPrint[ ], int iElementsCount ) { … }

17 Many faces of CONST In addition to using const to prevent modifications being propagated upwards, you can use it instead of #define! For example, instead of – #define SIZE 10 You can use –const int SIZE = 10 ;

18 Strlen Finds the length of a string. Can be created by looking for the \0

19 strcpy Copies a string The first parameter is the DESTINATION, and the first is the source

20 strcmp Compares two strings If they are the same word, returns 0, and will be a value other than 0 otherwise


Download ppt "Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld."

Similar presentations


Ads by Google