article

Intro to C/C++ Part 9: Strings

Email
Submitted on: 1/1/2015 7:42:00 AM
By: Alexander of CProgramming.com (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: C++ (general)
Views: 1046
 
     This lesson is one on strings. Strings are really arrays, but there are some different functions that are used for strings, like adding to strings, finding the length of strings, and also of checking to see if strings match. Strings are basically sentences, or words. Like, "This is a string".

 
				

Strings are basically character arrays. For example, to declare a string of 50 letters, you would want to say:

char string[50];

     This would declare a string with a length of 50 characters. Don't forget that arrays begin at 0, not 1 for the index-number. Also, a string ends with a null character, literally a '/0'  character. But, just remember that there will be an extra character on the end on a string. It is like a period at the end of a sentence, it is not counted as a letter, but it still takes up a space.

     What are strings useful for? Well, for one thing, you might want to get a person's name. If you wanted to, you would need a string, because a name can't fit in one variable! It is, however, a collection of characters, and so fits nicely into a character array. 

     Now, what if you want to input a string? Well, if you try to use cin>> then it won't work! It terminates at the first space. However, you can use the function gets(char *s);.  Gets is basically a function that reads in a string and stops reading at the first new-line, for example, when a user hits enter. Gets is in stdio.h. All you do, is put the name of the array and it will work out, because the pointer char *s is basically one way you tell a function that you will be passing an array, although it is a pointer, it is still the string you give. Essentially, char *s points to a string, and you can access this string just like an array.  I will touch upon this relationship as described above in another lesson that will be a more advanced lesson on pointers and arrays.

     Anyway, to get a string from a user you want to use gets. An example program of this would be:

#include <stdio.h> //For gets
#include <iostream.h> //For all other input/output functions

void main()

{

char astring[50]; //This declares a character array that can be used as a string
cout<<"Please enter a string"; //You should know this one!
gets(astring); //The user will input a string(with spaces)
cout<<"You input: "<<endl; //You know this one too!
cout<<astring; //This is how you output character arrays, but not others!
}

Ok, thats pretty simple. But, what if you want to use some of the nifty functions from string.h?  Well, these include the following functions:

int strcmp(const char *s1, const char *s2);

strcmp will accept two strings. It will return an integer. This integer will either be:

Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.

Strcmp is case sensitive.

int strcmpi(const char *s1, const char *s2);

strcmp will accept two strings. It will return an integer. This integer will either be:

Negative if s1 is less than s2.
Zero if the s1 and s2 are equal.
Positive if s1 is greater than s2.

Strcmpi is not case sensitive, if the words are capitalized it doesn't matter.

char *strcat(char *desc, char *src);

strcat is short for string cacatenate, which means to add to the end, or append. It does just this, the first string is what the second string is stuck on the end of. It basically returns the cacatenated string. The first string will also have the entire string added to it.

char *strupr(char *s);

strupr converts a string to uppercase. It also returns a string, which will all be in uppercase.  The input string, if it is an array, will also all be uppercase.

char *strlwr(char *s);

strlwr converts a string to lowercase. It also returns a string, which will all be in uppercase.  The input string, if it is an array, will also all be uppercase.

size_t strlen(const char *s);

strlen will return the length of a string, minus the termating character(/0). The size_t is nothing to worry about. Just treat it as an integer.  Some of the stuff in the strings may be confusing. The const char *s stuff, for example. But, just remember that basically all of that will be a string! It doesn't matter what the code is right now, just what the functions do.  Now, a small program using many of the string functions!

#include <iostream.h> //For cout

#include <string.h> //For many of the string functions

#include <stdio.h> //For gets

void main()

{

char name[50]; //Declare variables

char lastname[50]; //This could have been declared on the last line...

cout<<"Please enter your name: "; //Tell the user what to do

gets(name); //Use gets to input strings with spaces or just to get strings after the user presses enter

if(!strcmpi("Alexander", name)) //The ! means not, strcmpi returns 0 for equal strings

{ //strcmpi is not case sensitive

cout<<"That's my name too."<<endl; //Tell the user if its my name

}

else //else is used to keep it from always outputting cout<<"That's not my name."

{

cout<<"That's not my name."<<endl;

}

 

cout<<"What is your name in uppercase..."<<endl;

strupr(name); //strupr converts the string to uppercase

cout<<name<<endl;

cout<<"And, your name in lowercase..."<<endl;

strlwr(name); //strlwr converts the string to lowercase

cout<<name<<endl;

cout<<"Your name is "<<strlen(name)<<" letters long"<<endl; //strlen returns the length of the string

cout<<"Enter your last name:";

gets(lastname); //lastname is also a string

strcat(name, " "); //We want to space the two names apart

strcat(name, lastname); //Now we put them together,we a space in the middle

cout<<"Your full name is "<<name; //Outputting it all...

}


Other 10 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 Intermediate 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.