Comparing strings?

This is a discussion on Comparing strings? within the C Programming forums, part of the General Programming Boards category; Hello, I have a question about comparing strings. First, I will post the code I've written: Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    Question Comparing strings?

    Hello,
    I have a question about comparing strings.
    First, I will post the code I've written:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void make_word(char* s, char c)
    {
            int len = strlen(s);
            s[len] = c;
            s[len + 1] = '\0';
    }
    
    int main()
    {
            char grid [3][4] = {{'a', 'b', 'c'},
                                {'d', 'e', 'f'},
                                {'g', 'h', 'i'}};
    
            char word[] = "abc";
    
            char found_word[] = "";       //room for found_word
    
            int i;
    
            for(i = 0; i != 3; i++)
            {
                make_word(found_word, grid[i]);
            }
    
    
    
    
            if(word == grid[3])
            {
                printf("yes");
            }
    
    
    
    
            return 0;
    }
    So what I am trying to do is to compare two strings.
    In the code, there is an array grid and a string found_word.
    The string found_word is made from the array grid.
    The function allows the grid to become a string.
    So what I have done is make the 'a' 'b' 'c' into a string "abc", which is same as "abc" from the string word.
    So now the word contains "abc" and found_word also contains "abc"
    However, when I try the if loop, they are not the same.
    How do I make the array and the string the same so that when the if loop runs, it prints the "yes"?

    Please help

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    1.Allocate memory for char*s before using it....
    2.grid[i] is not a (char) ..it is a (char*)
    3.Your
    Code:
    void make_word(char*,char)
    function appends the character in the arguments after the string(as you meant it)..but found_word contains only a '\o' and grid[i] isn't a character..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all... it's int main (void)

    Second, your make word function is not a text search. All it does is add a 0 onto the end of a string.

    Third ... char found_word[] = ""; ... is NOT how you create text buffers. The result will be an array of 1 character containing a 0.

    Finally ... you cannot compare strings across the = sign in C. It doesn't know how to do that so you will need to find a library function (or write your own) to do that for you.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Quote Originally Posted by CommonTater
    First of all... it's int main (void)
    Does not matter here: like void, an empty parameter list in a function definition specifies that the function has no parameters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>Does not matter here: like void, an empty parameter list in a function definition specifies that the function has no parameters.

    In C++. But not in C
    For C, it means what parameter (number, type) is not specified.

    Code:
    void foo() 
    {
    
    }
    
    
    ...
     foo()
     foo(3,2);
     foo("hello", 2.32,3);
    This will compile cleanly in C. But not in C++.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Quote Originally Posted by Bayint Naung
    In C++. But not in C
    For C, it means what parameter (number, type) is not specified.
    We're talking about a function definition, not a function declaration that is not a function definition:
    Quote Originally Posted by C99 Clause 6.7.5.3 Paragraph 14
    An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.
    Quote Originally Posted by Bayint Naung
    This will compile cleanly in C.
    I have observed this before in gcc, but based on my understanding of the C standard, I believe that "compile cleanly" is a compiler bug. That said, as a rule of thumb explicit is better than implicit, and C99 does say that the use of empty parentheses in this way is an "obsolescent feature" (though whether it will really be removed from the language is another matter).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 07:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  4. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 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