Thread: strcpy

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    strcpy

    i was reading the tutorials on this website and got to strcpy. this is what it says(with my concern underlined)
    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.
    what i understand from this is that strcpy will return 0. well i tried it out with printf and it gave me <dest>. am i understanding the tutorial wrong? or do i understand the term "return" wrong? doesn't 5 * 6 return 30(as an example)? heres my src anyway
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
       int i;
       char string1[256], string2[256];
       
       printf("Enter first string: ");
       fgets(string1, 256, stdin);
       printf("Enter second string: ");
       fgets(string2, 256, stdin);
       for(i = 0; i < 256; i++) {
          if(string1[i] == '\n') string1[i] = '\0';
          if(string2[i] == '\n') string2[i] = '\0';
       }
       printf("%s", strcpy(string1, string2));
       getchar();
       return 0;
    }
    Last edited by Abda92; 09-13-2006 at 05:18 AM.

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    strcmp return 0 if strings are identicall, not strcpy. strcpy will return string1. Before getchar add this line
    Code:
    printf("\n%d\n", strcmp(string1, string2));
    Last edited by andor; 09-13-2006 at 05:43 AM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    the two string functions strcpy and strcmp are completely different functions.

    the difference is strcpy copies a string to an array, whereas strcmp compares the two strings for order.

    for example.
    Code:
    strcmp("HELLO", "WORLD") // returns -1
    strcmp("HELLO", "HELLO") // returns 0
    strcmp("WORLD", HELLO") // returns 1

    what the tutorial is saying is that when you copy a string into an array using strcpy and then cmp the two strings using strcmp, strcmp will return 0

    try the code below
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void) {
        char s1[] = "Hello World", s2[12];
        strcpy(s2, s1);
        printf("compare s1 and s2 = %d\n", strcmp(s1, s2)); // this prints ... = 0
        return 0;
    }
    read your quote again and it will be obvious.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    ok. i guess i'll have to be more careful when i'm studying. thank everyone, i really appreciate your help

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    how does strcmp work? peterchen mentioned that
    strcmp compares the two strings for order.
    why do the first, second and third examples return -1, 0 and 1 respectively?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Actually strcmp doesn't necessarily return 1 or -1 but a number less than or greater than 0 if they differ

    The strcmp() and strncmp() return an integer greater than, equal to, or
    less than 0, according as the string s1 is greater than, equal to, or
    less than the string s2.
    The first retuns a negative number because 'H' is less than 'W'
    The second returns 0 because all of the letters are equal
    The last retuns a positive number because 'W' is greater than 'H'

    Code:
    printf("%d", 'H' - 'W');
    It works by stepping though each character and comparing them, just like you would compare two strings on paper.
    Last edited by spydoor; 09-13-2006 at 08:54 AM.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    And to further what spydoor said, the reason 'H' is less than 'W' is that 'H' really is 72 (decimal) in ascii, and 'W' is 87 (decimal). If you want a look at the different ascii values associated with your keyboard characters, check this out.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM