Thread: What should I do????

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    What should I do????

    I used these codes to concatenate and compare the strings, but couldnt succeed. Please let me know if I m making any mistake. Thank you
    void concatenate_string(void) {

    char userstrings[5][80];
    int x;
    int y;
    printf("Which two strings would you like to concatenate\?\n");
    scanf("%d", &x);
    scanf("%y", &y);
    strncat(userstrings[x],userstrings[y],80);
    fputs("The two strings you concatenated are :\n",stdout);
    printf("%s%s", userstrings[x],userstrings[y]);
    return;
    }


    void compare_string (void) {
    char userstrings[5][80];

    int x;
    int y;

    printf("Which two strings would you like to compare\?\n");
    scanf("%d", &x);
    scanf("%d", &y);
    fputs("The two strings you compared are :\n",stdout);
    printf("%s", strncmp(userstrings[x],userstrings[y],80));
    if (userstrings[x]!=userstrings[y]){
    printf("%d\n",y");
    }
    else{
    printf("%d\n",y");
    }
    return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%y", &y);
    %y is not a scanf flag.

    >printf("%s%s", userstrings[x],userstrings[y]);
    userstrings[x] holds the concatenated strings, so that's all you have to print.

    >printf("%s", strncmp(userstrings[x],userstrings[y],80));
    This will print 0, 1, or -1, which is an integer, not a string. But then you want to use that value again in the test, a better way would be:
    Code:
    printf ( "The two strings you compared are :\n%s, %s", 
             userstrings[x], userstrings[y] ); 
    if ( strncmp ( userstrings[x], userstrings[y], 80 ) != 0 )
      /* Not equal */
    else
      /* Equal */
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    problem

    I want it to print 1 if the two strings are not equal and 0 if they are not. Pls help

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I want it to print 1 if the two strings are not equal and 0 if they are not.

    Code:
    printf ("%d\n", (strncmp (userstrings[x],userstrings[y],80) != 0) ? 1: 0);

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    sorry

    mistyped before,
    here is the question


    I want it to print 1 if the two strings are not equal and 0 if they are

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >> printf ("%d\n",
    >> (strncmp (userstrings[x],userstrings[y],80) != 0) ? 1: 0);
    >
    >
    > I want it to print 1 if the two strings are not equal and 0 if they
    > are

    Do you REALLY need help with this? Change the final 1 to a 0, and the 0 to a 1. Or hell, just do this:

    printf("%d\n", !!strcmp( userstrings[x],userstrings[y] ) );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed