lecture10%2Dpost

lecture10%2Dpost - Monday,November8 CStrings Midtermreview...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Monday, November 8 th C Strings Midterm review… 
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Classic C Strings So far, we have used C++  string variables  in two programming projects.  main() { string s = “hello”; cout << s << “ world!\n”; } If you are programming in C, you  can’t  use these C++ string variables!!!    Question : So how do you create string variables in C?
Background image of page 2
Classic C Strings In C programs we use a  character array variable  to hold a string.  After all, a string is just a group of characters, and  a char array holds  a group of chars . main() { char myname[ 10 ] = “ Carey ”; // a C string cout << “Hi “ << myname; }   A char array of size  N  can hold a C string up to  N-1  bytes  long.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Learning C Strings #include <string.h> // used for C strings main() { char myname[ 10 ] = “ Carey ”; // a C string char newname[ 20 ]; strcpy (newname, myname); cout << “Hi “ << newname; }   To copy one C string into another, use the  strcpy  function. Usage:  strcpy( destination source );     Note: You  cannot  just use the equal sign with C strings (unless you’re  initializing the string when you define it) newname = myname;  // ERROR! newname = “Joseph”;  // ERROR! Note : Make sure  that your  destination string is big enough  before you copy!    
Background image of page 4
Learning C Strings #include <string.h> // used for C strings main() { char myname[ 20 ] = “ Carey ”; // a C string strcat (myname, “ Bo-berry”); cout << “Hi “ << myname; // Hi Carey Bo-berry }   To append one C string onto another, use the  strcat  function. Usage:  strcat( destination source );     Note: You  cannot  just use the += operator with C strings. myname += “Bo-berry”;  // ERROR! char lastname[ 20 ] = “ Nachenberg ”; lastname Nachenberg Note : Make sure  that your  dest. string is big  enough before  you append
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Learning C Strings #include <string.h> // used for C strings main() { char myname[ 20 ] = “ Carey ”; // a C string cout << “Your name has “ << strlen (myname) << “ letters in it\n”; }   To get the length of a C string, use the  strlen  function. Usage:  strlen( str ); Note: You  cannot  use the .length() operator with C strings. cout << myname.length(); // ERROR!
Background image of page 6
Learning C Strings #include <string.h> // used for C strings main() { char myname[ 20 ]; // a C string cout << “Enter your name: “; cin >> myname; if (strcmp(myname,”Carey”) == 0) cout << “Welcome, Master\n”; }   To compare two C strings, use the  strcmp  function. strcmp(
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

lecture10%2Dpost - Monday,November8 CStrings Midtermreview...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online