"If the flag needs protection at all, it needs protection from members of Congress who value the symbol more than the freedoms that the flag represents." --U.S. Rep. Jerrold Nadler, D - NY
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:
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:
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/
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 12 comments.
HackThisSite is is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.
Page Generated: Wed, 22 May 2013 22:26:33 +0000 Web Node: www0 | Page Gen: 0.138s | DB: 10q Current Code Revision: Thu Dec 6 19:06:02 UTC 2012