Thread: search chain in string(pointers)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    search chain in string(pointers)

    hi. i have a work to do that consists on a given char pointer to a chain and a also given char pointer to a string. i have to search that chain in the string, and return the position where the beginning of the two it's equal. but only if they are equal.
    example:
    string is "abcd" and the chain is "bc". want to find the chain, and if it exists, the position return will be 2.
    in the work it says that we should use sprintf() too.
    here is my code, which is incomplet yet.

    Code:
    char* pos(char* C,char* S){
        char *str = S;
        char *chain = C;
        
        int i, j, temp;
        int lengthStr = strlength(C);
        int lengthChain = strlength(S);
        
        char *posit= malloc(4*sizeof(lengthStr));
    
        for(i=0; i < lengthStr; i++){
            if( *(str+i) == *chain){
                sprintf(posit++, "%d", i);
                for(j=0; j < lengthChain && *str==*chain; j++){
                    if( *(str) != *(chain))
                        posit--;
                    else{
                        str++;    
                        chain++;
                    }
                }
            }    
            //else
                //*posit = 0;                
        }
        free(posit);
        return posit;
    }
    my doubt is how to use sprintf() and where. if i have to use malloc() and free(), and maybe a little help for the code.
    thanks, and sorry if the text is confused

    other examples:
    pos(“AGA”,”ACTGCA”)
    return:
    0

    pos(“AGA”, “ATAGATA”)
    return:
    3

    pos(“AGA”, “AGATAGA”)
    return
    1 5

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    There are so many problems in your code that I'm pretty sure that you have just started coding without any plan. So I recommend that you turn off your computer and work out the algortihm you want to implement on paper. Imagine that you have to explain every step you do to someone who has no clue about finding a substring inside a string. So be as precise and clear as possible.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    i know there are problems, thats why im asking for help. maybe im doing this by the wrong way, some feedback would be helpful. thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *posit= malloc(4*sizeof(lengthStr));
    Why 4 times the number of chars?

    > sprintf(posit++, "%d", i);
    The ++ will advance by only 1 character, so as soon as you get into double digit numbers, you're stuck again.

    Code:
                    if( *(str) != *(chain))
                        posit--;
                    else{
                        str++;   
                        chain++;
                    }
    I would strongly suggest you find a way to do this using things like str[i] and chain[j]
    Mucking about with pointer arithmetic is just going to mess things up.

    For example, chain++ is just going to keep going forwards until you run out of string.

    > free(posit);
    > return posit;
    If you're returning it, you don't want to free it (that problem belongs to the caller).
    Also, you need to stop all the posit++ and posit-- stuff in the code, otherwise you're returning a pointer to somewhere within your result (if you're lucky).
    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.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    hi salem, thanks for advices. so, in the function sprintf i will never get double digit numbers, because i is always a int, right? the i it's for save the position where the begin of string and chain are equal. what i want to do is save that position, in case chain be a substring. i'm using pointer arithmetic because is supposed to use i guess, even the arguments of the function pos() are pointers. i guess i will forget malloc in this function. i'm going to modify the code and search for a better way to do this. how can i save the positions where chain it's a substring of the string? i must return a char pointer, and the use of sprintf is to save int digits in that char pointer right?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Tell me this

    int foo = 1234567;
    sprintf(posit++,"%d",foo);


    How many characters get printed?
    Where is posit now pointing?
    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.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    am i not puting the foo to the posit? posit points to nothing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. chain of constructors
    By Farnaz in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2011, 07:02 AM
  2. How would we chain functions?
    By chickenlittle in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2010, 09:21 AM
  3. Search with pointers
    By lio in forum C Programming
    Replies: 7
    Last Post: 11-04-2010, 09:14 AM
  4. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  5. pointers-string search
    By spveer in forum C Programming
    Replies: 2
    Last Post: 07-07-2005, 05:48 AM