Thread: Strings confusion, =/.

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Strings confusion, =/.

    I'm confused about how exactly the string functions (strcat, etc.) work. I honestly just don't understand them at all, although more specifically, I don't understand how or what the string functions do in the following code example that they give:

    Code:
    #include <iostream> //For cout
    #include <cstring>  //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<<"Please enter your name: ";
      cin.getline ( name, 50 );
      if ( strcmp ( name, "Julienne" ) == 0 ) // Equal strings
        cout<<"That's my name too.\n";
      else                                    // Not equal
        cout<<"That's not my name.\n";
      // Find the length of your name
      cout<<"Your name is "<< strlen ( name ) <<" letters long\n";
      cout<<"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<<"Your full name is "<< fullname <<"\n";
      cin.get();
    }
    If someone could please explain it to me a bit better than they do here, I would greatly appreciate it.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    strlen - Gives the length of a string
    strcpy - Copies one string to another string
    strcat - Copies one string onto the end of another string
    strcmp - Compares two strings, returns zero of there's no diff

    For more info, consult the man pages.

    Also, do you at least know about C++ string objects?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by UMR_Student View Post
    Also, do you at least know about C++ string objects?

    Nope, not the slightest clue about them.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    well they're a good way to go

    Code:
    #include <string>   <-- In C++
    and then it's just:
    Code:
    string whatever;
    you'll find that they are Extremely more dynamic and easier (in most respects) than char arrays.

    however, the ways in which you deal with them are different. But in your case, if you want to add two together you could do something like this:
    Code:
    string firstname;
    string lastname;
    string fullname;
    
    ... get info here...
    
    fullname = firstname + " " + lastname;
    That ought to do it

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Awesome, thanks very much to both of you.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I think that char fullname[100]; must be char fullname[101];! Because if someone write a name and lastname which both contains 50 chars. Plus the space between the names. Then there is 100 + 1 = 101.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, neither first name nor last name can be longer than 49 characters. Therefore the full name cannot be longer than 98 characters + space + null terminator = 100.

    Another good reason to switch to C++ strings.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yup =) Strings are much better for this sort of thing.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM