jdfbghjdf

Embed Size (px)

Citation preview

  • 7/29/2019 jdfbghjdf

    1/4

    Lesson 9: Strings

    This lesson is one on strings. Strings are really arrays, but there aresome differentfunctions that are used for strings, like adding to strings, finding the lengthof strings, andalso 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, youwould want to say:

    char string[50];

    This would declare a string with a length of 50 characters. Don't forget that arrays begin at0, 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 endon a string. Itis like a period at the end of a sentence, it is not counted as a letter, but itstill takes up

    a space.

    What are strings useful for? Well, for one thing, you might want to get a person's name. If youwanted to, you would need a string, because a name can't fit in one variable! It is, however, acollection 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 itwon't work! Itterminates 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, forexample, 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 anarray, although it is a pointer, it is still the string you give. Essentially,char *s points toa 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 moreadvanced lesson on pointers and arrays.

    Anyway, to get a string from a user you want to use gets. An example program ofthis would be:

    #include //For gets#include //For all other input/output functions

    void main(){

  • 7/29/2019 jdfbghjdf

    2/4

    char astring[50]; //This declares a character array that can be

    used as a stringcout

  • 7/29/2019 jdfbghjdf

    3/4

    size_t strlen(const char *s);

    strlen will return the length of a string, minus the termating character(/0). The size_t isnothing to worry about. Just treat it as an integer.

    Some of the stuff in the strings may be confusing. The const char *s stuff, forexample. But,just remember that basically all of that will be a string! It doesn't matter what the code isright now, just what the functions do.

    Now, a small program using many of the string functions!

    #include //For cout#include //For many of the string functions#include //For gets

    void main(){char name[50]; //Declare variableschar lastname[50]; //This could have been declared on the last line...cout

  • 7/29/2019 jdfbghjdf

    4/4

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