{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture10 - Monday,November8 CStrings Midtermreview th...

Info icon This 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… 
Image of page 1

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

View Full Document Right 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?
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.
Image of page 3

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

View Full Document Right 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!    
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
Image of page 5

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

View Full Document Right 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!
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.
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern