Thread: function and strings - C

  1. #16
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Thanks for your answers
    @zacs
    this is not homework. I found the exercise(as many others) in a site and i am trying to do it for practise only

    i will try your tips and if i have a problem i will post again.So see you soon in a few hours

  2. #17
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by zacs7 View Post
    Since this is homework (probably) you're not getting the answer, but here is a pretty good start:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    static void printUnique(const char * a, size_t m, const char * b, size_t n);
    
    int main(void)
    {
        char str1[] = "Hello";
        char str2[] = "Hallo";
    
        /* what's in str1 that isn't in str2 ? */
        printUnique(str1, strlen(str1), str2, strlen(str2));
    
        /* what's in str2 that isn't in str1 ? */
        printUnique(str2, strlen(str2), str1, strlen(str1));
    
        return 0;
    }
    
    static void printUnique(const char * a, size_t m, const char * b, size_t n)
    {
        size_t i, f;
        int found = 0;
    
        /* print unique from a that aren't in b */
        for(i = 0; i < m; i++)
        {
            found = 0;
            for(f = 0; f < n; f++)
            {
                if(a[i] == b[f])
                {
                    found = 1;
                    break;            /* no point checking the rest */
                }
             }
            if(found == 0)
                printf("%c is in a but not b...\n", a[i]);
        }
        return;
    }
    
    /* output:
    e is in a but not b...
    */
    If you have to use pointers just use a pointer to point at a[i], ie

    Code:
    char * ptr;
    
    /* ... */
    for(i = 0; i < m; i++)
    {
        ptr = a[i];
        printf("value is %c...\n", *ptr);
    /* ... */
    Yes i think this way will work too, its a kind of brute force attack and it is great. But why not to use strpbrk whick imho does the same job. Well its your choice anyway.

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    mistake
    Last edited by alzar; 09-15-2007 at 05:17 AM.

  4. #19
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    i made another try.Still i don't use malloc but an array.If this code work after i write it with malloc.The problem is that it doesn't print me nothing
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char ar[20];
    
    void fun(char *s1, char *s2);
    
    int main(void)
    {
     char *p1="hellosw";
     char *p2="ahellogw";
    
     fun(p1,p2);
    
     printf(ar);
     return 0;
    }
    
    void fun(char *s1, char *s2)
    {
     int i,j,l;
     int found;
     int slen1,slen2;
    
     slen1=strlen(s1);
     slen2=strlen(s2);
    
     found=0; 
     l=0;
    
     for(i=0; i<slen1; i++)
     { for(j=0; j<slen2; j++)
       { if(s1[i]==s2[j]) /*for each element of s1[i] i check if there is also in s2*/
           found=1;
       }
      if (found=0)  
        ar[l++]=s1[i]; /*if not i copy the character to array ar*/
        
    }
    
    }
    where is the mistake?

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use
    Code:
    if (found==0)
    or just
    Code:
    if (!found)
    Kurt

  6. #21
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    still doesn't print anything with found==0

  7. #22
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    for(i=0; i<slen1; i++)
     { 
       found = 0;
       for(j=0; j<slen2; j++)
       { if(s1[i]==s2[j]) 
          { found=1; break;}
           
       }
      if (found==0)
          ar[l++]=s1[i]; 
    }
    ssharish

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    yes now prints me the hole first string and i don't want that
    i think that in the loops i am making these:
    if s1="het" and s2="aht"
    for i=0
    for j=0
    s1[0]=s2[0] (h=a) (false)
    for j=1
    s1[0]=s2[1] (h=h) (true and break)

    for i=1
    for j=0
    s1[1]=s2[0] (e=a) (false)
    for j=1
    s1[1]=s2[1] (e=h) (false)
    for j=2
    s1[1]=s2[2] (e=t) (false)
    then
    found is still 0
    I am putting e in ar[0]

    etc.

    Does my code do these things that i am describing to you or not?
    If yes then why don' t print what i want?

  9. #24
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i think you are looking for this

    Code:
     for(i=0; i<slen2; i++)
     { 
       found = 0;
       for(j=0; j<slen1; j++)
       { if(s1[j]==s2[i]) /*for each element of s1[i] i check if there is also in s2*/
          { found=1; break;}
           
       }
      if (found==0)
      {  
        ar[l++]=s2[i]; /*if not i copy the character to array ar*/
      }
    ssharish

  10. #25
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to reset found to 0 inside the outher loop.
    Kurt

  11. #26
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    @ssharish
    i want the characters from first string to copy to ar.BUT these characters that i copy should not be in second string(s2)
    example
    s1= hello
    s2= hallo

    then ar has only e because h,l and o are also in s2

    so with your code ar[l++]=s2[i];
    i don't think that does what i want

    @kurt
    what do you mean? this?
    Code:
    for(i=0; i<slen1; i++)
     { 
       found = 0;
       for(j=0; j<slen2; j++)
        if(s1[i]==s2[j])
    because i make it and don't work as i want
    Last edited by alzar; 09-17-2007 at 02:18 PM.

  12. #27
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char ar[20];
    
    void fun(char *s1, char *s2);
    
    int main(void)
    {
     char *p1="hello";
     char *p2="hallo";
    
     fun(p1,p2);
    
     printf(ar);
     getchar();
     return 0;
    }
    
    void fun(char *s1, char *s2)
    {
     int i,j,l;
     int found;
     int slen1,slen2;
    
     slen1=strlen(s1);
     slen2=strlen(s2);
    
     found=0; 
     l=0;
    
     for(i=0; i<slen1; i++)
     { 
       found = 0;
       for(j=0; j<slen2; j++)
       { if(s1[i]==s2[j]) /*for each element of s1[i] i check if there is also in s2*/
          { found=1; break;}
           
       }
      if (found==0)
      {  
        ar[l++]=s1[i]; /*if not i copy the character to array ar*/
      }  
    }
    
    }
    
    /* my output
    e
    */
    You get the output. What else u need?

    ssharish

  13. #28
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    YES why do you use getchar??
    In my code i don't use getchar.
    Can you explain me?
    With getchar the code runs as i want but why?

  14. #29
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Because i need to Stop my Windows Console from disappearing everytime I run my program. Since I am using Dev-C++.

    You could ignore that, if you want. If you are on console.

    ssharish

  15. #30
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Thanks i found the problem .It works also without getchar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM