Lesson 9: Strings This lesson is one on strings. Strings are really arrays, but there are some different functions that are used for strings, like adding to strings, finding the length of strings, and also of checking to see if strings match. Strings are basically sentences, or words. Like, "This is a string". Strings are basically character arrays. For example, to declare a string of 50 letters, you would want to say: char string[50]; This would declare a string with a length of 50 characters. Don't forget that arrays begin at 0, not 1 for the index-number. Also, a string ends with a null character, literally a '/0' character. But, just remember that there will be an extra character on the end on a string. It is like a period at the end of a sentence, it is not counted as a letter, but it still takes up a space. What are strings useful for? Well, for one thing, you might want to get a person's name. If you wanted to, you would need a string, because a name can't fit in one variable! It is, however, a collection of characters, and so fits nicely into a character array. Now, what if you want to input a string? Well, if you try to use cin>> then it won't work! It terminates at the first space. However, you can use the function gets(char *s); . Gets is basically a function that reads in a string and stops reading at the first new-line, for example, when a user hits enter. Gets is in stdio.h. All you do, is put the name of the array and it will work out, because the pointer char *s is basically one way you tell a function that you will be passing an array, although it is a pointer, it is still the string you give. Essentially, char *s points to a string, and you can access this string just like an array. I will touch upon this relationship as described above in another lesson that will be a more advanced lesson on pointers and arrays. Anyway, to get a string from a user you want to use gets. An example program of this would be: #include //For gets #include //For all other input/output functions void main() { char astring[50]; //This declares a character array that can be used as a string cout<<"Please enter a string"; //You should know this one! gets(astring); //The user will input a string(with spaces) cout<<"You input: "< //For cout #include //For many of the string functions #include //For gets void main() { char name[50]; //Declare variables char lastname[50]; //This could have been declared on the last line... cout<<"Please enter your name: "; //Tell the user what to do gets(name); //Use gets to input strings with spaces or just to get strings after the user presses enter if(!strcmpi("Alexander", name)) //The ! means not, strcmpi returns 0 for equal strings { //strcmpi is not case sensitive cout<<"That's my name too."<