Differences

This shows you the differences between the selected revisions of the page.

2005-02-25 2005-02-26
no summary (24.225.202.101) (hidden) no summary (68.100.85.145) (hidden)
Line 20: Line 20:
[[ui_login_ssh|&screenshotpict(login_ssh)]] [[ui_login_ssh|&screenshotpict(login_ssh)]]
-<html><!-- #BeginTemplate "/Templates/c_template2.dwt" --> 
-<head><link rel="stylesheet" href="http://www.cprogramming.com/images/style.css" type="text/css"> 
-<meta http-equiv="name" content="C and C++ game programming"> 
-<META NAME="Keywords" CONTENT="C++ programming, beginnings guide to C++, C++ beginners guide, C++ programming tutorials, getting started with C++, learning C++, how to learn C++, history of C++, C++ history, programming languages, C++ programming language">  
-<META NAME="Description" CONTENT="A beginner's guide to learning C++ - answers questions about C++, basic programming, and how to think about programming.">  
-<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
-<!-- #BeginEditable "doctitle" -->  
-<meta http-equiv="name" content="C and C++ game programming"> 
-<meta name="keywords" 
-content="C++ programming, programmers, C++ tutorial, C, source code, tutorials, C++ compiler, DJGPPorials "> 
-<meta name="description" 
-content="Cprogramming.com is a combination of C++ tutorials, compiler information, programming links, a VERY ACTIVE programming message board, and C and C++ source code ."> 
-<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
-<TITLE>Cprogramming.com Tutorial: Strings</TITLE> 
-<!-- #EndEditable --> 
-</head> 
-<body link="#000000" vlink="#000000" alink="#000000"> 
- 
- 
-<a href="http://www.cprogramming.com/"> 
- 
- 
- 
-<img class="logo" src="http://www.cprogramming.com/images/title.gif" width="324" height="83" border="0">  
- 
- 
- 
-</a>  
- 
- 
- 
-<table class="line"> 
- 
- <tr> 
- 
- <td> </td> 
- 
- </tr> 
- 
-</table> 
- 
- 
- 
-<div class="form" style="vertical-align:middle" align="right"> 
- 
-<!-- SiteSearch Google --><form method="get" action="http://www.google.com/custom" target="google_window"><input type="hidden" name="domains" value="cprogramming.com"></input><input type="text" name="q" size="31" maxlength="255" value=""></input> <input type="submit" name="sa" value="Search"></input><input type="hidden" name="sitesearch" value="cprogramming.com"></input><input type="hidden" name="client" value="pub-2560316224908115"></input><input type="hidden" name="forid" value="1"></input><input type="hidden" name="ie"value="ISO-8859-1"></input><input type="hidden" name="oe"value="ISO-8859-1"></input><input type="hidden" name="cof"value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1;"></input><input type="hidden" name="hl" value="en"></input></form><!-- SiteSearch Google --> 
- 
-</div> 
- 
- 
- 
-<div> 
- 
-  <table class="main"> 
- 
-    <tr> 
- 
-  <td colspan="5" bgcolor="#EBEBEB"> 
- 
-  <img src="http://www.cprogramming.com/images/spacer-vert.gif" height="6" width="0"> 
- 
-    <center> 
- 
-    <div class="leaderboard"> 
- 
-<Script language="Javascript1.1" src="http://tag.contextweb.com/TagPublish/getntag.aspx?action=VIEWAD&run=200&cwformat=728x90&advid=111&pid=400490&width=728&height=90&pnet=1"></Script> 
- 
-</div> 
- 
-</center> 
- 
- <img src="http://www.cprogramming.com/images/spacer-vert.gif" height="4" width="1"> 
- 
-  </td> 
- 
-  </tr> 
- 
-  <tr> 
- 
-  <td class="divider-horiz" colspan="5"> 
- 
-  </td> 
- 
-  </tr> 
- 
-  <tr> 
- 
-      <td width="135" valign="top" bgcolor="#EBEBEB">  
- 
-        <center> 
- 
-    <div class="menu" valign="top"> 
- 
-    <p class="navcategory" align="left"> 
- 
-          <img class="bullet" src="http://www.cprogramming.com/images/bullet.gif" width="11" height="13">    Starting out 
- 
-    </p> 
- 
-    <p class="navlist" align="left"> 
- 
-    <a href="http://www.cprogramming.com/begin.html">Getting Started</a><br> 
- 
-    <a href="http://www.cprogramming.com/tutorial.html">Tutorials</a><br> 
- 
-    <a href="http://www.cprogramming.com/cgi-bin/quiz.cgi">Quizzes</a> 
- 
-    </p> 
- 
-    <p class="navcategory" align="left"> 
- 
-          <br> 
- 
-          <img class="bullet" src="http://www.cprogramming.com/images/bullet.gif" width="11" height="13"> Moving on 
- 
-    </p> 
- 
-    <p class="navlist" align="left"> 
- 
- <a href="http://www.cprogramming.com/advtutorial.html">Advanced Tutorials</a><br>  
- 
-            <a href="http://www.cprogramming.com/articles.html">Articles</a><br> 
- 
-    <a href="http://www.cprogramming.com/challenge.html">Challenges</a><br> 
- 
-    <a href="http://contests.cprogramming.com">Contests</a><br><a href="http://www.cprogramming.com/tips/">Tips and Tricks</a> 
- 
-    </p> 
- 
-    <p class="navcategory" align="left"> 
- 
-          <br> 
- 
-          <img class="bullet" src="http://www.cprogramming.com/images/bullet.gif" width="11" height="13"> Tools 
- 
-    </p> 
- 
-    <p class="navlist" align="left"> 
- 
-    <a href="http://www.cprogramming.com/tools.html">What do I need?</a><br> 
- 
-    <a href="http://www.cprogramming.com/compilers.html">Compilers</a><br> 
- 
-    <a href="http://www.cprogramming.com/texteditors.html">Editors</a><br> 
- 
-    <a href="http://www.cprogramming.com/debuggers.html">Debuggers</a> 
- 
-    </p> 
- 
-    <p class="navcategory" align="left"> 
- 
-          <br> 
- 
-          <img class="bullet" src="http://www.cprogramming.com/images/bullet.gif" width="11" height="13"> Resources 
- 
-    </p> 
- 
-    <p class="navlist" align="left"> 
- 
- <a href="http://www.cprogramming.com/cgi-bin/source/source.cgi">Source Code</a><br><a href="http://www.cprogramming.com/reference/">Syntax Reference</a><br><a href=http://www.cprogramming.com/snippets/>Snippets</a><br> 
- 
-    <a href="http://www.cprogramming.com/cgi-bin/cdir/Cdirectory.cgi">Links Directory</a><br> 
- 
-    <a href="http://www.cprogramming.com/cgi-bin/glossary/glossary.pl">Glossary</a><br> 
- 
-    <a href="http://www.cprogramming.com/books.html">Book Reviews</a><br> 
- 
-    <a href="http://www.cprogramming.com/function.html">Function Lookup</a> 
- 
-    </p> 
- 
-    <p class="navcategory" align="left"> 
- 
-          <br> 
- 
-          <img class="bullet" src="http://www.cprogramming.com/images/bullet.gif" width="11" height="13"> Questions 
- 
-    </p> 
- 
-    <p class="navlist" align="left"> 
- 
-    <a href="http://faq.cprogramming.com/">Programming FAQ</a><br> 
- 
-    <a href="http://www.cprogramming.com/board.html">Message Board</a><br> 
- 
-    <a href="http://www.cprogramming.com/expert.html">Ask an Expert</a><br> 
- 
-    <a href="http://www.cprogramming.com/email.html">Email</a> 
- 
-    </p> 
- 
-    </div> 
- 
- 
- 
-      <!-- AI Horizon Link Image START --> 
- 
-      <a href="http://www.aihorizon.com/" 
- 
-      onMouseout="window.status=''; return true" onMouseover="window.status='An Affiliate of AIHorizon'; return true"> 
- 
-      <br> 
- 
-      <br> 
- 
-      <img src="http://cprogramming.com/Images/aihorizon.gif" width="90" height="34" alt="An Affiliate of AIHorizon" border="1"></a> 
- 
-      <!-- AI Horizon Link Image END --> 
- 
-    <br> 
- 
-    <br> 
- 
-      <!-- The Hunger Site Link START --> 
- 
-      <a href="http://www.thehungersite.com"> 
- 
-      <img src="http://www.cprogramming.com/graphics/hs.gif" width=90 height=45 border=0> 
- 
-      </a> 
- 
-    <br> 
- 
-    <br> 
- 
-    </center> 
- 
-    </td> 
- 
-   
- 
-    <td class="divider"></td> 
- 
- 
- 
- <td class="content" valign="top"> 
- 
-     
-        <div class="content2">  
-          <p> <!-- #BeginEditable "content" --><font class="h1">Lesson 9: Strings</font> (<a href="http://www.cprogramming.com/tutorial/print/lesson9.html">Printable Version</a>) 
-In C++ there are two types of strings, C-style strings, and C++-style strings. This lesson will discuss C-style strings. C-style 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. The definition of a string would be anything that contains more than one character strung together. For example, "This" is a string. However, single characters will not be strings, though they can be used as strings. 
- 
-<br><br>  
-Strings are arrays of chars. String literals are words surrounded by double quotation marks. 
-<pre class="example">  
-"This is a static string" 
-</pre>  
-To declare a string of 49 letters, you would want to say: 
-<pre class="example">  
-char string[50]; 
-</pre>  
-This would declare a string with a length of 50 characters. Do not forget that arrays begin at zero, not 1 for the index number. In addition, a string ends with a null character, literally a '\0' character. However, 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. Technically, in a fifty char array you could only hold 49 letters and one null character at the end to terminate the string. 
-<br><br>  
-TAKE NOTE: char *arry;  
-Can also be used as a string. If you have read the tutorial on pointers, you can do something such as: 
-<pre class="example">  
-arry = new char[256]; 
-</pre>  
-which allows you to access arry just as if it were an array. Keep in mind that to use delete you must put [] between delete and arry to tell it to free all 256 bytes of memory allocated. 
-<br><br>  
- 
-For example: 
-<pre class="example">  
-delete [] arry. 
-</pre>  
-Strings are useful for holding all types of long input. If you want the user to input his or her name, you must use a string. Using cin&gt;&gt; to input a string works, but it will terminate the string after it reads the first space. The best way to handle this situation is to use the function cin.getline. Technically cin is a class (a beast similar to a structure), and you are calling one of its member functions. The most important thing is to understand how to use the function however.  
-<br><br>  
-The prototype for that function is: 
-<pre class="example">  
-istream& getline(char *buffer, int length, char terminal_char); 
-</pre>  
-The char *buffer is a pointer to the first element of the character array, so that it can actually be used to access the array. The int length is simply how long the string to be input can be at its maximum (how big the array is). The char terminal_char means that the string will terminate if the user inputs whatever that character is. Keep in mind that it will discard whatever the terminal character is. 
-<br><br>  
-It is possible to make a function call of cin.getline(arry, 50); without the terminal character. Note that '\n' is the way of actually telling the compiler you mean a new line, i.e. someone hitting the enter key.  
-<br><br>  
- 
-For a example: 
-<pre class="example">  
-#include &lt;iostream&gt; 
- 
-using namespace std; 
- 
-int main() 
-{ 
-  char string[256];                              // A nice long string 
- 
-  cout&lt;&lt;"Please enter a long string: "; 
-  cin.getline ( string, 256, '\n' );              // Input goes into string 
-  cout&lt;&lt;"Your long string was: "&lt;&lt; string &lt;&lt;endl; 
-  cin.get(); 
-} 
-</pre>  
-Remember that you are actually passing the address of the array when you pass string because arrays do not require an address operator (&) to be used to pass their address. Other than that, you could make '\n' any character you want (make sure to enclose it with single quotes to inform the compiler of its character status) to have the getline terminate on that character. 
-<br><br>  
-cstring is a header file that contains many functions for manipulating strings. One of these is the string comparison function. 
- 
-<pre class="example">  
-int strcmp ( const char *s1, const char *s2 ); 
-</pre>  
-strcmp will accept two strings. It will return an integer. This integer will either be: 
-<pre class="example">  
-Negative if s1 is less than s2. 
-Zero if s1 and s2 are equal. 
-Positive if s1 is greater than s2. 
-</pre>  
-Strcmp is case sensitive. Strcmp also passes the address of the character array to the function to allow it to be accessed. 
-<pre class="example">  
-char *strcat ( char *dest, const char *src ); 
-</pre>  
-strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents. 
-<pre class="example">  
-char *strcpy ( char *dest, const char *src ); 
-</pre>  
-strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0. 
-<pre class="example">  
- 
-size_t strlen ( const char *s ); 
-</pre>  
-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 that cannot be negative, which it is. 
-<br><br>  
-Here is a small program using many of the previously described functions: 
-<pre class="example">  
-#include &lt;iostream&gt; //For cout 
-#include &lt;cstring&gt;  //For the string functions 
- 
-using namespace std; 
- 
-int main() 
-{ 
-  char name[50]; 
-  char lastname[50]; 
-  char fullname[100]; // Big enough to hold both name and lastname 
-   
-  cout&lt;&lt;"Please enter your name: "; 
-  cin.getline ( name, 50 ); 
-  if ( strcmp ( name, "Julienne" ) == 0 ) // Equal strings 
-    cout&lt;&lt;"That's my name too.\n"; 
-  else                                    // Not equal 
-    cout&lt;&lt;"That's not my name.\n"; 
-  // Find the length of your name 
-  cout&lt;&lt;"Your name is "&lt;&lt; strlen ( name ) &lt;&lt;" letters long\n"; 
-  cout&lt;&lt;"Enter your last name: "; 
-  cin.getline ( lastname, 50 ); 
-  fullname[0] = '\0';            // strcat searches for '\0' to cat after 
-  strcat ( fullname, name );    // Copy name into full name 
-  strcat ( fullname, " " );      // We want to separate the names by a space 
-  strcat ( fullname, lastname ); // Copy lastname onto the end of fullname 
-  cout&lt;&lt;"Your full name is "&lt;&lt; fullname &lt;&lt;"\n"; 
-  cin.get(); 
-} 
- 
-</pre>  
- <br><br> 
-<a href="quiz/quiz9.html"> Quiz yourself</a><br> 
-<a href="lesson8.html"> Previous: Arrays</A><br> 
-<a href="lesson10.html"> Next: File I/O</a><!-- #EndEditable --> </p> 
-           
-<p align="center"> -----<br> 
- 
-            Interested in <a href="http://www.cprogramming.com/advertising.html">advertising  
- 
-            with us</a>?<br> 
- 
-            Please read our <a href="http://www.cprogramming.com/privacy.html">privacy  
- 
-            policy</a>.<br> 
- 
-            <a href="http://www.cprogramming.com/use.html">Copyright</a> © 1997-2005  
- 
-            Cprogramming.com. All rights reserved.<br> 
- 
-            Design by <a href="http://www.geodesydesign.com/">Geodesy Designs</a>  
- 
-        </div> 
- 
-    </td> 
- 
-     
- 
-     
- 
- <td class="divider" valign="top" rowspan="1"> 
- 
-    </td> 
- 
-     
- 
-     
- 
- <td width="150" class="verticalad" valign="top" rowspan="1" bgcolor="#EBEBEB"> 
- 
- <div class="content2"><br><div class="verticalad"> 
- 
- <Script language="Javascript1.1" src="http://tag.contextweb.com/TagPublish/getntag.aspx?action=VIEWAD&run=200&cwformat=120x600&advid=111&pid=400490&width=120&height=600&pnet=1"></Script> 
- 
-<noscript><a href="http://www.burstnet.com/ads/ba10219a-map.cgi/ns/v=1.0J/sz=468x60B/" target="_top"> 
- 
-<img src="http://www.burstnet.com/cgi-bin/ads/ba10219a.cgi/ns/v=1.0J/sz=468x60B/" border="0" alt="Click Here"></a>  
- 
-</noscript> 
- 
-<!-- END BURST CODE --></div><br> 
- 
-    </center>  
- 
-  </td>   
- 
-  </tr> 
- 
-</table> 
- 
-  </div> 
- 
-</body> 
- 
-<!-- #EndTemplate --></html> 

Last modified: by 68.100.85.145