Thread: unsure of how to use the functions strcmp and strncmp

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    unsure of how to use the functions strcmp and strncmp

    I am writing a few programs in C. I am stuck. i am not sure how to use strcmp or strncmp. i cant find any websites that can help and my book does not give any good examples. This is what i have to do.

    Frist..... Write a program that inputs a line of text with function gets into char array s[100]. Output the line in uppercase letters and in lowercase letters.

    Second....Write a program that uses function strcmp to compare two strings input by the user. The program should state whether the first string is less than, equal to or greater than the second string.

    Third..... Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to be compared. The program should state whether the first string is less than, equal to or greater than the second string.

    These 3 programs should be combined into 1 program.


    I am stuck and here is what i have so far. I have completed the first program but i am having trouble with the second and third program. Can you help me with? or maybe steer me in the right direction ? Thanks I appreciate it.


    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    # define SIZE 80
    
    int main()
    {
        int i;
            char s[SIZE];
    
            /* prompt user to enter line of text */
            puts(  "Enter line of text:\n" );
            gets(s);
    
            /* use gets to display sentence */
            printf("Upper case lines:\n");
                    for (i=0; i < strlen(s); i++)
                            printf("%c", toupper(s[r]));
    
                    printf("\n");
    
            printf("Lower case line:\n");
            for(i=0; i < strlen(s); i++)
                    printf("%c", tolower(s[r])):
    
                    print("\n");
    
                    char s1{80};
                    char s2{80};
                    int x;

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    - first, read this and make the necessary changes. send the link to your teacher, too.

    - how come you set size to 80 when it says to use 100?
    - i think you mean to use square braces in the 3rd and 2nd last lines of your code above

    i am not sure how to use strcmp or strncmp. i cant find any websites that can help
    that is a lie! best thing to read is it's man page. first notice the #include statement that you must add. next look at the function prototype:
    Code:
    int strcmp(const char *s1, const char *s2);
    so the function returns an integer which you should save after calling the function (read the Return Values section to see what the int value returned means). you also must pass two strings (char*'s / char[]'s). these would, i bet, be the two strings you just recieved as input from the user.

    you would then want to use some if-else structure to compare which string is larger, etc, and do the appropriate statements as specified in your requirements.

    the strncmp function is similar, but you only want to compare x number of characters with the second string, where x is some integer read from the user. youll likely have to do some string manipulation (search for string.h help to see what functions are available!).

    after you get part 2 working and have an attempt at 3, we can further help.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by hellgrammite View Post
    Frist..... Write a program that inputs a line of text with function gets into char array s[100]. Output the line in uppercase letters and in lowercase letters.
    gets() is considered deprecated, and something that should not be used. If a college or university wants to show you how it works, but they teach you to never use it in real code, that's fine. Again, you should never actually use it in real code.

    Code:
    # define SIZE 80
    
    ...
    
    char s[SIZE];
    If they asked you to make an array of size 100, then why are you making it of size 80?

    Code:
    printf("%c", toupper(s[r]));
    ...
    printf("%c", tolower(s[r])):
    Where did r come from?

    Code:
    char s1{80};
    char s2{80};
    Braces are not used for array declarations.

    Google for information on strcmp() and strncmp().

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MacGyver View Post
    gets() is considered deprecated, and something that should not be used. If a college or university wants to show you how it works, but they teach you to never use it in real code, that's fine.
    Respectfully disagree. It's not fine. What's the additional complexity of fgets()? So you have to pass a FILE* and a buffer size... That is not that hard. If students wonder what it means ("What's this 'stdin' thing?"), wrap it in a function like GetString() and give that to them to use.

    Teaching them gets() is just horrible. I don't think the onus should be upon students to determine WHICH of the things they are being taught are proper, and which things are a bunch of crap. I believe we should teach only good practices. If these good practices are complex, "wrap and provide." Then they get the added bonus of an example of good encapsulation of complexity to boot.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I said I didn't mind them teaching it as long as, "they teach you to never use it in real code".

    I'm also not totally against the notion of teaching students what goto is and how it works, again, as long as they also teach it's a horrible practice to pull it out for no reason.

    You could argue that they should never be taught gets() but only fgets() That's fair enough, but gets() is all over the place in code and examples, and I think it should be something consciously fought against. If students are not aware as to why gets() is considered bad, then what is the motivation to avoid using it in code they are either writing themselves or borrowing from someone else?

    Perhaps we are just seeing it in different ways. You have my respect either way.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you're supposed to teach students how to write safe programs, then you should teach them right from the beginning. No gets! The function is evil and should be deprecated.
    It's amazing how many students using gets or other unsafe functions with little understanding of what they do -_-
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I do think brewbuck has an idea when he said wrap and provide, however. There's no reason why the instructor can't write something like that himself, and explain it to the students as part of the lesson plan. If you make the differences from gets apparent in the discussion, than that's even better - everybody is happy. Until you get to that point, fail the students that can't follow the spec and just use the function.

    They get the valuable skills they need to work on antiquated code bases written in turbo c.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but we are not supposed to see students using gets in code they're using for homework or such here. We are supposed to see fgets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > These 3 programs should be combined into 1 program.
    So your first program looks like
    Code:
    int main ( ) {
      /* your upper and lower code */
      return 0;
    }
    Then you change it into this
    Code:
    void doLowerUpper ( ) {
      /* your upper and lower code */
    }
    
    int main ( ) {
      doLowerUpper();
      /* add doStrcmp() and doStrncmp() as well */
      return 0;
    }
    Essentially rename each main() you create, then put a call to it from another main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proofing existing functions
    By pxleyes in forum C Programming
    Replies: 17
    Last Post: 04-05-2004, 04:31 PM
  2. seg fault with strcmp
    By samps005 in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 04:32 PM