Not working

This is a discussion on Not working within the C Programming forums, part of the General Programming Boards category; The program is suppose to look thought str and see if part can be found inside. problem is that it ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,157
    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%.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07: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, 07: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

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