Thread: programming assignment help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    programming assignment help

    Ok, I was assigned to do certain exercises out of my programming book for my class, I am having a bit of trouble and would appreciate some help. Here's the question:

    "Let f be the following function:
    Code:
    int f(char *s, char *t)
    {
      char *p1, *p2;
    
      for (p1 = s; *p1; p1++)  {
        for (p2 = t; *p2; p2++)
          if (*p1 == *p2) break;
        if (*p2 == '\0') break;
      }
      return p1-s;
    }
    (a) What is the value of f ( "abcd" , "babc") ?
    (b) What is the value of f ( "abcd" , "bcd") ?
    (c) In general, what values does f return when passed two strings s and t?"

    I put this code in a program and ran it myself. I may have did it wrong but for (a) I got 3 and for (b) I got 0. I don't really understand what is going on here and I would appreciate if someone could explain (c) to me.

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    IT is finding the first charcter in s which is not present in t and returning its position

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, thank you. I still don't understand how it is doing that but I will keep trying to figure it out. thanks for the help tho.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    int f(char *s, char *t)
    {
      char *p1, *p2;
    
      for (p1 = s; *p1; p1++)  {
        for (p2 = t; *p2; p2++)
         { if (*p1 == *p2) break;
          }
        if (*p2 == '\0') break;
      }
      return p1-s;
    }
    may be this will clear the code to you.it will find the last character in s that is different in t.sorry for the mistake in previous reply.
    the inner for loop is running untill the value is equal the first for loop is incremented....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM