help with strings

This is a discussion on help with strings within the C Programming forums, part of the General Programming Boards category; I am not getting what the loops are doing in the following function concerning strings: Code: int alpha(char *mom, char ...

  1. #1
    Unregistered
    Guest

    Question help with strings

    I am not getting what the loops are doing in the following function concerning strings:

    Code:
    int alpha(char *mom, char *pop)
    
    {
        char *m1, *p1;
    
           for (m1 = mom; *m1; m1++)      // what is this doing???
              {
                 for (p1 = pop; *p1; p1++)
                       if (*m1 == *p1) break;        // what does this mean??
                    if (*p1 == '\0') break;          
                                                              // why break at these points?
             }
           
           return m1 - mom;                        // what is m1 - mom???
    }
    assume values are called these different ways:::

    alpha("rstu", "wxyz") what is alpha now??
    alpha("rstu","wxy") what is alpha now??
    i see the one string is shorter
    than the other
    when alpha is passed both strings mom and pop what does alpha return???

    Please just help me get the notation in the for loops
    and what happens with the line return m1 - mom ....

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    what the whole function is actually doing is, it is comparing the strings and seeing how many of the letters are actually the same and returns the number of letters that are the same from the start. i.e try these and check the number returned.

    "rstu","wxyz" (returns 0)
    "wstu","wxyz" (returns 1)
    "wxtu","wxyz" (returns 2)
    "wxyu","wxyz" (returns 3)
    "wxyz","wxyz" (returns 4)

    Code:
    #include <stdio.h>
    
    int alpha(char *mom, char *pop){
        char *m1, *p1;
           for (m1 = mom; *m1; m1++){
                 for (p1 = pop; *p1; p1++){//make sure u always put these in
                     if (*m1 == *p1) break;
                 }
                 if (*p1 == '\0') break;
             }
           return m1 - mom;
    }
    
    int main(){
        int s;
        s=alpha("wxyz","wxyz");
        printf("%d\n",s);
        getchar();
    }
    this is the code i used

  3. #3
    Unregistered
    Guest

    Angry explain the for and if loops

    Kwigibo

    Could you explain the loops for me???

    Code:
    #include <stdio.h>
    
    int alpha(char *mom, char *pop){
        char *m1, *p1;
           for (m1 = mom; *m1; m1++){     //what is this actually doing?
                 for (p1 = pop; *p1; p1++){
                    if (*m1 == *p1) break;              //is the == operator
                 }                                                    //  comparing elements
                 if (*p1 == '\0') break;                    // at the pointers           
             }
           return m1 - mom;       //  and what is returning, step by step?
    }
    
    int main(){
        int s;
        s=alpha("wxyz","wxyz");
        printf("%d\n",s);
        getchar();
    }

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Angry

    I tried to do this and get a Segmentation Fault (whatever that is)

    Where is my error???

    Code:
    #include <stdio.h>
    int f(char *, char *);
    main()
    {
       char *p1, *p2;
          printf("here's your output :\n");
           f(p1,p2);
           puts(p1);
           puts(p2);
    }
        
    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;
        }
    HERE'S MY OUTPUT :::::

    here's your output :
    Segmentation Fault (core dumped)
    Sue B.

    dazed and confused


  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    In your example, you declare p1 and p2, but you never actually assign them to any strings. So they're just pointing off at some random point in memory. Assign them to strings before you call f.

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. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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