strcat or strcpy?

This is a discussion on strcat or strcpy? within the C++ Programming forums, part of the General Programming Boards category; hi and thanks for your rsponces ,but im stock again at the nd of my progrand, waht i need to ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    14

    strcat or strcpy?

    hi and thanks for your rsponces ,but im stock again at the nd of my progrand, waht i need to do is concanate first 2 characters of one string to another, it does it fine but i get a whole bunch of other character at the end of it plus the same string again, how do i stop that from happening here is some of my code, i have included <cstring>




    Code:
     
    int main()
    {
    	char string1[10];
    	char string2[15];
    	char string3[15];
    	char string4[8];
    
    cout <<"Enter string1: ";
    cin.getline(string1, 10, '\n');
    cout <<"Enter string2: ";
    cin.getline(string2, 15, '\n');
    
    cout<<endl<<string1<<" is "<<strlen(string1)<<" characters in length"<<endl;
    cout<<string2<<" is "<<strlen(string2)<<" characters in length"<<endl;
    
    cout<<endl<<string1<<" is "<< sizeof(string1) << " characters in size"<<endl;
    cout<<string2<<" is "<< sizeof(string2) << " characters in size"<<endl; 
    
    if (strcmp(string1,string2) !=1 || 0)
    {
    cout<<endl<<string1<<" is less than  "<<string2<<endl;
    	
    }		
    	
    
    cout<<endl<< "string3 after concatenation is "<<strcat(string1,string2)<<endl;
    
    
    cout<<endl<<"String 4 is "  <<strncpy(string2,string1,3)<<endl;
    
    
    cout<<endl<<"String 1 is now " <<strncpy(string2, string1,7)<<endl;
    
    return 0;
    }

    im entering billy for string 1
    bob for string 2

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    strncpy doesn't add a NULL at the end unless there's more room, strcat is better.

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    You have to ensure that string1 is large enough to accomodate the characters of both string1 AND string2. All strcat does is copy the contents from one string onto the end of the contents of another string. It doesn't add more memory onto the end of string1 to accomodate the new data.

  4. #4
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    i didn't really read the code, or the question (sorry), but i think this line

    if (strcmp(string1,string2) !=1 || 0)

    won't give you the results you want.. if you want to test if strcmp returns 1 or returns 0, you needs to do

    Code:
    if((strcmp(string1, string2) != 1) || (strcmp(string1, string2) != 0))
    {
       feedTheFish();
    }
    //edit: haHA! i win
    Last edited by MadHatter; 12-09-2002 at 07:08 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Pioneer
    strncpy doesn't add a NULL at the end unless there's more room, strcat is better.
    ... but they do different things!

    >>if (strcmp(string1,string2) !=1 || 0)
    This is a syntax problem, did you mean something more like this:
    >>if (strcmp(string1,string2) <= 0)

    [edit]dammit, beaten!

    [edit2]I've closed this thread that has exactly the same question on it. Please don't start new threads when there is no need.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by MadHatter
    Code:
    if((strcmp(string1, string2) != 1) || (strcmp(string1, string2) != 0))
    I doubt this is what he wants, since this is a tautology (always true). You could as well have if(true) instead, since it gives the same result.

    Use > 0 if the first string should be larger than the second
    Use < 0 if the first string should be smaller than the second
    Use == 0 if both strings should be equal
    Use >= 0 and/or <= for a combination of the above
    Use != 0 if either larger or smaller, but not equal
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    yea, i wasn't sure about which values are returned when.. i just wasn't sure what values he needed, and it looked like that was what he was going for.. oh well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 03:49 AM
  2. problem in my strcpy strcat function
    By genie in forum C Programming
    Replies: 7
    Last Post: 11-07-2008, 09:36 AM
  3. Replies: 2
    Last Post: 06-03-2008, 01:57 AM
  4. strcpy() and strcat()
    By Moony in forum C Programming
    Replies: 5
    Last Post: 07-03-2006, 02:18 AM
  5. strcat - strcpy
    By pizzas in forum C Programming
    Replies: 4
    Last Post: 08-05-2003, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21