Need help with comparing strings (I'm a beginner C programmer)

This is a discussion on Need help with comparing strings (I'm a beginner C programmer) within the C Programming forums, part of the General Programming Boards category; I'm very new to C programming. My only other programming experience is Python Programming. I have an assignment in my ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    7

    Need help with comparing strings (I'm a beginner C programmer)

    I'm very new to C programming. My only other programming experience is Python Programming. I have an assignment in my programming class that involves comparison of strings, so I figured I'd write a little test program to see if I'm capable of doing that...
    So far I've been unsuccessful.... I tried to compare two strings, but even if they appear exactly the same, my program doesn't seem to agree.

    Here's the code...

    Code:
    #include <stdio.h>
    int
    main(void)
    {
            char* string;
            char* comp = "dummy";
    
            printf("Enter a string: ");
            scanf("%s", string);
    
            printf("The string you entered is %s.\n", string);
            printf("The string you are comparing to is %s.\n", comp);
    
            if(string == comp)
                    printf("The strings match!\n");
            else
                    printf("The strings don't match!\n");
    
            return (0);
    }
    And here's what my output looks like...

    Code:
    Enter a string: dummy
    The string you entered is dummy.
    The string you are comparing to is dummy.
    The strings don't match!
    I don't know what I'm doing wrong. >_< Can anyone help me?

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Bandizzle View Post
    I'm very new to C programming. My only other programming experience is Python Programming. I have an assignment in my programming class that involves comparison of strings, so I figured I'd write a little test program to see if I'm capable of doing that...
    So far I've been unsuccessful.... I tried to compare two strings, but even if they appear exactly the same, my program doesn't seem to agree.

    Here's the code...

    Code:
    #include <stdio.h>
    int
    main(void)
    {
            char* string;
            char* comp = "dummy";
    
            printf("Enter a string: ");
            scanf("%s", string);
    
            printf("The string you entered is %s.\n", string);
            printf("The string you are comparing to is %s.\n", comp);
    
            if(string == comp)
                    printf("The strings match!\n");
            else
                    printf("The strings don't match!\n");
    
            return (0);
    }
    And here's what my output looks like...

    Code:
    Enter a string: dummy
    The string you entered is dummy.
    The string you are comparing to is dummy.
    The strings don't match!
    I don't know what I'm doing wrong. >_< Can anyone help me?
    strcmp()
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Wow, that was fast! Thank you so much! I feel stupid now...but then again the strcmp() function is found over 100 pages ahead of where we're supposed to be at in our C textbook. I should read ahead lol. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner help about strings
    By yildiz.a in forum C Programming
    Replies: 41
    Last Post: 05-18-2009, 10:41 PM
  2. comparing strings: case INsensitive
    By bchan90 in forum C Programming
    Replies: 7
    Last Post: 12-10-2008, 05:54 AM
  3. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  4. comparing strings
    By Frantic- in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2004, 11:38 AM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 10:41 PM

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