This tutorial is for mostly beginners that need to know how to manipulate char strings easily. I recently switched from vb, and it was very hard to get used to chars (from vb's String identifier). This will show you how copy, compare, and add strings together. This will help you have an almost "VB-Like" handle on strings. This is for all uses of strings (Win32 and Console). KEYWORDS: Compare COMPARE compare Add ADD add COPY copy Copy CHARS chars char CHAR Char VB vb STRINGS Strings strings STRING string String tutorial Tutorial TUTORIAL Combine Combining
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Now, I have just switched from vb, and I know how stressful it is to get a grasp on strings using the char identifier. Even if you didn't, you still want to know how to manipulate strings easily right?
STRCMP // Used to compare strings
Lets say we made a simple password program, and we obviously needed to compare the real password against the password that the user entered. We would do this using an int variable, the strcmp method, and an if statement.
#define sRealPassword "JAIME" // The real password
#include
int main()
{
int icompare; // The actual result
char sInput[50];
cout << "What is your name? : ";
cin >> sInput;
icompare = strcmp(sInput, sRealPassword);
if (icompare == 0) // if the strings match
{
cout << "Welcome\n";
}
That is it. All we did was compare the strings, and then took the result, and if it was equal, then do what we want.
STRCAT // To add strings together
This is what most of us want. This is how, in vb , you would say sString = "Jaime " + "Muscatelli".
I just used this today, after wanting to find how to do this for 2 months! I finally figured it out, and now I want to help others with the same dilemma. I used for my "Windows Enumeration" example here on PSC, where when a user double clicks on a list item, it will ask them "Do you wish to close 'Window name here'". 99% of programs use this string combination. For example, when you exit Microsoft Word, doesn't it ask you to save and then the file name.
This is just the usage, not a complete program...
char sMessage[256];
char sFileName[256];
strcpy(sMessage,"Are you sure you wish to close ");
strcat(sMessage,sFileName);
strcat(sMessage, "?");
MessageBox(NULL,sMessage,0,0,0); //
Now, what we did here was assign the "Are you sure..." text into the sMessage variable (used strcpy), and then used strcat to add the data from sFileName to the sMessage variable. In other words, in vb this would look like:
sMessage = sMessage + sFileName.
Very simple right? Ok, we learned how to compare strings, assign data to them, and also add data to an already exisiting string. That is it! That was pretty simple. I am sorry for any grammatical errors. I know this is a lot more complex, yet I wanted to simplify it. There are more functions, yet these are crucial to c/c++.
Email Me with questions or comments. ALso, don't forget to check out my other submissions, and please leave comments and VOTE!!!!!
You might want to look up the STL string class. It's standard C++ and provides all (and more) of the functionality you are used to with VB strings. (If this comment was disrespectful, please report it.)
Thanks, I'm a VB progger making a DirectX program, and that just helped me alot. Now I need to search for conversion. lol The books I bought don't have that, well, they got strcat and strcpy but they don't talk a lot about them. The other books are for Visual C++ and they use things like... CString MyString; MyString.Format("Thats %s %s %s %s %s!", "the", "way", "they", "do", "it");
I don't use MFC and that is for MFC. Anyways, it isn't complete enough, 4 globes.
What is that STL thing? (If this comment was disrespectful, please report it.)
I've been programming for a while, and I couldn't find a very good explanation for strings anywhere. Yours told me exactly what I needed to know. Thanks a lot. (If this comment was disrespectful, please report it.)
Cheers; I too have also been wondering how to add strings together. Been using sprintf() till now ta :) (If this comment was disrespectful, please report it.)
Okay, very useful, really! But how do you add a number from e.g. an integer variable to the end of a string? Can anyone help me? (If this comment was disrespectful, please report it.)
2/12/2005 10:12:10 PM:
do you know how 2 arrange the letters of the name alphabetically using c++?...i badly need to know!..tanx
(If this comment was disrespectful, please report it.)
To Add a number to a string : convert the number to string using atoi() function and concatenate it with the actual string using strcat() function. (If this comment was disrespectful, please report it.)
To Add a number to a string : convert the number to string using itoa() function and concatenate it with the actual string using strcat() function. Sorry that was a typo! (If this comment was disrespectful, please report it.)
ather way to add an integer value to the end is cout << string << " " << integer
if the string was hi and the integer was 5 the you would see hi 5 on the screen.
nice tutorial by the way (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)