+ Reply to Thread
Results 1 to 4 of 4

Thread: Strings (CrashCourse)

  1. #1
    LogicKills's Avatar
    LogicKills is offline Programmer
    Join Date
    May 2008
    Location
    US
    Posts
    138
    Rep Power
    16

    Strings (CrashCourse)

    Working With Strings
    <br>

    Before We Get Started:
    Not only is C++ super efficient with numbers, it can also do quite a lot with strings. A string can be anything from "program" to "hello my name is Patrick and I am ..." . Get the idea? Lets start off with the first type I will be going over and these are 'cstrings', or C-Style strings. Lets take a look at some code and then we can tear it apart and see how it functions.
    Code:
    #include <iostream>
    
    //  using namespace std;
    // ^^^ Can use that instead of std::
    int main()
    {
        char myCstring[10] = "Hello"; // a C-Style string..
        
        std::cout << myCstring << std::endl;
        
        
        return 0;
    }
    Ok, lets analyze this code.
    The only thing that should look unfamiliar is the line with: 'char myCstring...'.

    This is where we are declaring our string. All the a cstring consists of is an array of type char, or characters. The special thing about cstrings are that the last character looks something like this '\0'. This would be the last character of all strings, it is rightfully named the 'null character'.
    This 'null character' is used by various functions that need to be able to handle strings. For example cout << would read HELLO and stop at the null character.
    Declaring a cstring like we did is called a string literal it includes the null character so you don't have to remember to put it. The tedious way to declare a string is: char test[13]={'H','e','l','l','o','\0'};
    -_- ... Doesn't look to fun eh, so lets stick to string literals for now..

    When we declare a cstring the size of the array is usually left blank. This is for saftey precautions really, and the reason why goes beyond the range of this article. If you would like to know more about that research buffer overflows. So the safe thing to do is to let the compiler say how much it needs by leaving the size blank: char test[ ] = "leave me blank";.

    Things You Can Do With CStrings...

    strlen() - Finds the strings length
    sizeof() - Finds the strings size in bytes.

    Lets mess with some code with these functions:
    Code:
    #include <iostream>
    #include <cstring> // need this for those functions :]
    
    // lets go ahead and declare the namespace globally
    
    using namespace std;
    
    int main()
    {
        const int str_size = 20; // We use this for our array size
        char myName[str_size];
        
        cout << "Why don't you enter your first name: ";
        cin  >> myName;
        cout << "Did you know your name is " << strlen(myName) << " characters long? ";
        cout << "\n";
        cout << "Well if you did know that, I bet you didn't know it is " << sizeof(myName) << "bytes";
        cout << "\n";
       
        
        return 0;
    }
    Basically we just used those functions I mentioned earlier. Both of them take 'myName' as an argument. Say we wanted the user to input multiple lines of text consisting of multiple words.. Well cin >> won't cut it there.. Here is an example.

    Code:
    #include <iostream>
    #include <cstring> // need this for those functions :]
    
    // lets go ahead and declare the namespace globally
    
    using namespace std;
    
    int main()
    {
        const int str_size = 20; // We use this for our array size
        char myName[str_size];
        char fvColor[str_size];
        
        cout << "Enter your first and last name: ";
        cin  >> myName;
        cout << "Enter your favorite color: ";
        cin  >> fvColor;
        cout << "It seems that " << fvColor << " is your favorite color, " << myName << endl;
      
        
        
        return 0;
    }
    Here is the input and output::

    Enter your first and last name:

    logic kills

    Enter your favorite color:

    green

    It seems that kills is your favorite color logic

    Meh.. not what we wanted..
    To fix this use cin.getline(myName,str_size);
    The reason this happened lies within the inner workings of 'cin'.
    When enter text their isn't a null character key on your keyboard, well at least not on mine anyways. So it still needs a way to find the end of the string. The way it does this is by: whitespace, newlines, or tabs.

    PART II -The String Class-

    C++ includes a class called string, it is actually also a type. To use it you have to include the <string> header file in your programs, it is also part of the std namespace. I personally prefer, and this all should prefer the string class.
    It is a lot safer and easier to work with, not to mention all the cool things it can do. Not that I am bagging on C programmers, C strings can do most of the things string types can, it just comes a little easier using a built in type rather than an array of char.

    Lets look at some of the things the string class can do..
    Code:
    #include <iostream>
    #include <string> //You guessed it..
        
    using namespace std; // Lets go ahead and declare it globally again..
    
    int main()
    {
        string test;   // an empty string ready for input;
        string assnstr = "I am a string..."; // Declaring a string literal..
    
    
       //You can assign one string to another like this..
        test = assnstr;
        cout << test << endl;
        
        //find the length of the string 
        cout << test << " is " << test.size() << " characters long counting the space. \n";
       
       //You can append strings on strings...
       test += " fearrrr meee ";
       cout << test << endl;
        return 0;
    }
    Their is so much you can do with strings, I just wanted to give you a taste.

    Note To Readers: All my code was compiled and ran on Slackware Linux , however it should compile on anything. Also, Windows users if your output flashes by too fast add some of these.. :
    Code:
    cin.ignore();
    cin.get();
    Hope everyone enjoyed..

    Here is some code to play with:
    Code:
    #include <iostream>
    #include <string> //You guessed it..
        
    using namespace std; // Lets go ahead and declare it globally again..
    
    int main()
    {
        string word;
        
        cout << "Enter a word:";
        getline(cin,word);
        cout << " ";
        
        for (int i = word.size(); i >= 0; i--)
        cout << word[i];
        
        cin.ignore();
        cin.get();
        return 0;
    }
    /LogicKills/
    http://logickills.org
    Science - Math - Hacking - Tech

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Strings (CrashCourse)

    Nice introduction to stringz I must say
    +repp
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  4. #3
    Join Date
    Jun 2007
    Location
    Kosovo
    Posts
    660
    Rep Power
    23

    Re: Strings (CrashCourse)

    Great tutorial +rep u could also include the ctype control for using touppper and tlower statment im sorry for my bad english respect to
    LogicKills for this greate tutorial hope i see a lot more tutorials from you so i can improve my knowledge in c++

  5. #4
    Jaywalker is offline Newbie
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Re: Strings (CrashCourse)

    The second Cstring one is puzzling me. Why does the window close when I hit "enter" or "return"?

    :S

    EDIT: These are all excellent tutorials btw, They have helped me so much just by going over them a couple of times.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C# Strings
    By chili5 in forum CSharp Tutorials
    Replies: 3
    Last Post: 08-21-2011, 02:23 PM
  2. C++ Strings
    By xXAlphaXx in forum C and C++
    Replies: 4
    Last Post: 03-04-2010, 03:36 PM
  3. Pointers(CrashCourse)
    By LogicKills in forum C Tutorials
    Replies: 30
    Last Post: 11-28-2008, 08:43 AM
  4. C Strings, C++ Strings?
    By telboon in forum C and C++
    Replies: 20
    Last Post: 11-20-2008, 06:16 PM
  5. Strings.
    By Aereshaa in forum C Tutorials
    Replies: 4
    Last Post: 10-15-2008, 05:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts