Thread: Not working

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    Not working

    The program is suppose to look thought str and see if part can be found inside. problem is that it doesnt do that. can someone help me fix it?
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    
    int main ()
    {
      char str[][17] = { "ATTCGCTTATTCCGAC" , "AAGCGCTAACCGCTAG" , "CAGCGCAAACGCCGTT" , "GCGCTCAAAGGCTTAA" };
      char part[17]="GCGCA";
      int n;
      for (n=0 ; n<4 ; ++n)
      {
        if (strcmp (str[n],part, 5) == 0)
        {
    		printf ("found %s\n",str[n]);
        }
    
      }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Did you compile it?

    What were the error messages - something about strcmp() not having 3 parameters perhaps?

    Did you perhaps intend to use strncmp() ?
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sounds like a job for strstr().
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Quote Originally Posted by Salem View Post
    Did you compile it?

    What were the error messages - something about strcmp() not having 3 parameters perhaps?

    Did you perhaps intend to use strncmp() ?
    i noticed that and fixed it but it still doesnt print anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you were thinking of strstr then.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    i am not allowed to use strstr. but i was told i can use points. the problem it i have no idea how to use pointers in this problem

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code is required to find a substring, but you are only comparing the strings.

    Try writing down on paper how you would find if any of the str[n]'s contain a substring that is equal to part. In particular, ponder the difference between "find substring" and "compare strings".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you check manually to see if your short code actually exists in the longer ones?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps they want you to fake strstr() by calling strncmp() in a loop.
    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. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM