Strings 10 24 2007 1 Opening Discussion Let s look at solutions to the interclass problem Denim Group presentation in HAS 340 at 5pm Answers to the minute essay a int b int b 2 int b int a int Do you have any questions about the assignment 2 Strings I m deviating from the order of the book for this because I want us to be able to use strings We ll go into more detail later A string in C is a null terminated array of characters That is to say it is an array of characters with the value zero stored after the last character Note this is the value zero not the character zero Since zero is not a valid character in ASCII this can be used to end the string 3 Initializing String Variables When you declare an array of chars you can set it equal to a string literal You don t have to provide the length of the array unless you want extra space in the string 4 Strings as Pointers Many functions will treat strings as a char or a const char instead of a char As we ll see later this makes sense because arrays and pointers are extremely similar in many ways When you write a function that deals with a string you can have it take either a char or a char I will probably use char out of habit 5 Writing some String Functions Let s write one or two functions that deal with strings 6 String Library Obviously many people need these functions As you would expect they exist in a library for exactly this reason You can include string h and look in the man pages to see all the different things that the C libraries allow you to do with strings While it is good to know how to manipulate strings you will probably use the libraries for most basic manipulations 7 String I O In printf and scanf you use s to signify a string For scanf it only reads one word and you don t need to put the in front of the variable because an array is like a pointer The fgets function can be used to read in a full line 8 Minute Essay Write a function that takes a string as input and returns the number of lower case letters in the string Denim Group presentation in HAS 340 at 5pm Interclass Problem Play with strings some and write a program that has the user input a string then does some manipulations and prints out an altered string You can pick the details 9
View Full Document