Você está na página 1de 4

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 w ords. Like, "This is a string". Strings are basically character arrays. For example, to declare a strin g 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 a rrays begin at 0, not 1 for the index-number. Also, a string ends with a null character, liter ally 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 perso n's name. If you wanted to, you would need a string, because a name can't fit in one variable! I t 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 fir st new-line, for example, when a user hits enter. Gets is in stdio.h. All you do, is put the nam e of the array and it will work out, because the pointer char *s is basically one way you tell a function that you wi ll 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 wi ll 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 <stdio.h> #include <iostream.h> void main() { //For gets //For all other input/output functions

char astring[50]; used as a string

//This declares a character array that can be

cout<<"Please enter a string"; //You should know this one! gets(astring); //The user will input a string(with spaces) cout<<"You input: "<<endl; cout<<astring; not others! } Ok, thats pretty simple. But, what if you want to use some of the nifty functio ns from string.h? Well, these include the following functions: int strcmp(const char *s1, const char *s2); strcmp will accept two strings. It will return an integer. This integer will e ither be: Negative if s1 is less than s2. Zero if s1 and s2 are equal. Positive if s1 is greater than s2. Strcmp is case sensitive. int strcmpi(const char *s1, const char *s2); strcmp will ither be: Negative if Zero if the Positive if accept two strings. It will return an integer. This integer will e s1 is less than s2. s1 and s2 are equal. s1 is greater than s2. //You know this one too! //This is how you output character arrays, but

Strcmpi is not case sensitive, if the words are capitalized it doesn't matter. char *strcat(char *desc, char *src); strcat is short for string cacatenate, which means to add to the end, or append. It does just this, the first string is what the second string is stuck on the end of. It basically returns the cacatenated string. The first string will also have the entire string added to it. char *strupr(char *s); strupr converts a string to uppercase. It also returns a string, which will all be in uppercase. The input string, if it is an array, will also all be uppercase. char *strlwr(char *s); strlwr converts a string to lowercase. It also returns a string, which will all be in uppercase. The input string, if it is an array, will also all be uppercase.

size_t strlen(const char *s); strlen will return the length of a string, minus the termating character(/0). T he size_t is nothing to worry about. Just treat it as an integer. Some of the stuff in the strings may be confusing. The const char *s stuff, for example. But, just remember that basically all of that will be a string! It doesn't matter wh at the code is right now, just what the functions do. Now, a small program using many of the string functions! #include <iostream.h> #include <string.h> #include <stdio.h> 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 equa l strings { //strcmpi is not case sensitive cout<<"That's my name too."<<endl; //Tell the user if its my name } else //else is used to keep it from always output ting cout<<"That's not my name." { cout<<"That's not my name."<<endl; } cout<<"What is your name in uppercase..."<<endl; strupr(name); //strupr converts the string to uppercase cout<<name<<endl; cout<<"And, your name in lowercase..."<<endl; strlwr(name); //strlwr converts the string to lowercase cout<<name<<endl; cout<<"Your name is "<<strlen(name)<<" letters long"<<endl; //strlen returns the length of the string cout<<"Enter your last name:"; gets(lastname); //lastname is also a string strcat(name, " "); //We want to space the two names apart strcat(name, lastname); //Now we put them together,we a space in the middle cout<<"Your full name is "<<name; //Outputting it all... } //For cout //For many of the string functions //For gets

Note: My homepage is http://www.cprogramming.com. My email is lallain@concentri c.net. Please

email me with comments and or suggestions. If you want to use this on your own site please email me and add a link to http://www.cprogramming.com. Thanks :)

Você também pode gostar