Important alert: (current site time 7/15/2013 5:58:17 PM EDT)
 

article

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

Email
Submitted on: 2/22/2003 12:25:49 PM
By: Jaime Muscatelli  
Level: Beginner
User Rating: By 10 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 31821
author picture
(About the author)
 
     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...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				// 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 5 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

2/22/2003 3:53:40 PMS Halayka

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

 
5/15/2003 9:59:49 AMMark N

Halayka's right. just include (no .h)
(If this comment was disrespectful, please report it.)

 
7/1/2003 7:37:41 AMWolf McCloud

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

 
8/11/2003 4:09:13 AMHyperHacker

Ya realize your <stdio.h> doesn't show up because it's being used as html...
(If this comment was disrespectful, please report it.)

 
11/6/2003 10:21:11 AMJAmes

I am Trying to write a dll for Labview but i keeps causing errors when I use strcat can anyone sugest any other way of joining strings

(If this comment was disrespectful, please report it.)

 
3/27/2004 10:41:30 PMRich Abdill

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

 
6/11/2004 3:15:20 AMHellOnComputer

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

 
6/11/2004 3:21:39 AMHellOnComputer

JAmes:

char str[256];
sprintf(str,"%s%s",strone,strtwo);

that'll work, not sure if theres another way; apart from that of strcat().
(If this comment was disrespectful, please report it.)

 
11/23/2004 3:57:05 PMpassword

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

 
6/27/2005 8:11:23 PMDerek Skeba

Good job.
(If this comment was disrespectful, please report it.)

 
7/24/2005 2:06:40 AMKarthik A

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

 
7/24/2005 2:08:55 AMKarthik A

Good Tutorial. Keep it up.
(If this comment was disrespectful, please report it.)

 
7/24/2005 3:10:56 AMKarthik A

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

 
9/26/2008 10:10:00 PMian

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

To post feedback, first please login.