string

This is a discussion on string within the C Programming forums, part of the General Programming Boards category; Can somebody explian to me whty this function will return 7 ? Code: int foo(char *s1, char *s2) { int ...

  1. #1
    Unregistered
    Guest

    Question string

    Can somebody explian to me whty this function will
    return 7 ?


    Code:
    int foo(char *s1, char *s2)
    {
       int c=0, s, p, found;
    
       for (s=0; s1[s] != '\0'; s++)
       {
          for (p=0, found=0; s2[p] != '\0'; p++)
          {
             if (s2[p] == s1[s])
             {
                found = 1;
                break;
             }
          }
          if (!found) c++;
       }    
       return c;
    }
    If we were to make the following call to foo what value would it return?

    foo(“Asia Pacific”, “aeiou”);

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The function returns the number of characters from the first argument that do not match any characters from the second argument. It should return 7:

    "Asia Pacific" == 12 (not including the nul)

    Matches:
    'i', 'a', 'a', 'i', 'i' == 5

    Non-Matches:
    'A', 's', ' ', 'P', 'c', 'f', 'c' == 7

    Don't forget that a space is a character too.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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