article

_A Beginner's STRCMP, STRCAT, and STRCPY tutorial (VB-LIKE String Manipulation)

Email
Submitted on: 1/5/2015 12:16:00 AM
By: Jaime Muscatelli (from psc cd)  
Level: Beginner
User Rating: By 10 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 3909
 
     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

 
				// Jaime Muscatelli

// webmaster@jaimemuscatelli.zzn.com
// AOL SN: Jaime141974

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!!!!!


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

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.)
 

To post feedback, first please login.