3 Replies - 788 Views - Last Post: 13 May 2013 - 02:48 PM Rate Topic: -----

#1 pulp_fiction  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-February 13

Delete a substring from a string(custom version) not working.

Posted 12 May 2013 - 10:08 PM

Hey guys
I am a beginner C programmer. I just stumbled upon this question(more of a challenge):

//start of question.
Write a function that deletes a portion of a string.Here is its prototype:
int del_substr(char *str,char const *substr);

The function should first determine whether the string substr occurs in str.If it does not,the value 0
should be returned .If the substring does apperar,the function should remove it by copying the characters in str
that follow the substring over the substring itself.The value 1 should then be returned.If the substring appears several times in the first argument,only the first occurrence should be deleted.The second argument should be changed.
TO illustrate,suppose str points to ABCDEFG.If substr points to FGH,CDF,or XABC,the function should change return 0 and leave str unchanged.But if substr points to CDE,the function should change str to ABFG by copying the characters F,G, and the NUL byte.The function should then return 1.In no event is the second argument string ever modified.
CONSTRAINTS:
a) You may not use any of the library routines(strcpy,strcmp,etc) and,
B)/> You may not use any subscripts in your function.
One more thing:The empty string is a substring of every string.
//End of the question.

I wrote my custom code to this question.It may not be optimized w.r.t speed or memory usage.(I always end up writing
inefficient code)
The problem with my code is,when I try ABCDEFGH as the main string and CDE as substring,program returns somw garbage value.I even dry ran the code in my mind but to no avail.
Here is the code I have written,I have commented wherever possible and explained my logic in the comment box:
/*Logic used:
I have compared every character of  the main str[] with the first character of substr[].--line 32
Then while both of them are non zero and equal,I just kept moving. The break statement
will take you out of this while loop(Those if,else if,else won't be executed now).
Lets say the substring is there in the main string.This process will stop when either of them is '\0'
Now the if,else if,else handles the possible cases.You can easily visualize this process
on a test string.*/
[code]#include<stdio.h>

int del_substr(char *str,char const *substr);
int main()
{
    char str[50],substr[50];
    int ret_hold;
    printf("Enter the main string\n");
    gets(str);
    printf("Enter the sub string\n");
    gets(substr);
    ret_hold=del_substr(str,substr);
    if(ret_hold==1)
        printf("Occurence Found\n");
    else
        printf("No Occurence\n");
    return 0;
}

int del_substr(char *str,char const *substr)
{
    char *str_p1=str,*substr_p1=substr;
    //str_p1 is a pointer to hold the start of str[] .Similarly substr_p1 is for substr[]
    if(substr=='\0')
        return 1;     //Empty string is a subset of every string.
    if(str=='\0')
        return 0;     //substr[] is not empty but main string str[] is empty.
    while(str!='\0')               //Till the end of main string(its every character)
    {
        if(*str==*substr)          //current character of str[] and first character of substr[]
        {
            while(*++str_p1!='\0'&&++substr_p1!='\0')   //While both of them are non '\0'
            {
                if(*str_p1==*substr_p1)
                    ;
                else
                    break;          //If while in comparison equality is not satisfied.
            }
            if(*str_p1=='\0'&&*++substr_p1!='\0')
            {
                return 0;           //substr[] is still left but str[] is over.So substr[] is not part of str[]
            }
            else if(*str_p1=='\0'&&*++substr_p1=='\0') /*substr[] is synchronized with the end of the main string str[]*/
            {
                str_p1=str;
                while(str_p1!='\0')
                {
                    *str_p1='\0';
                    str_p1++;
                }
                return 1;        //Denotes success of finding substr[] in str[]
            }
            else
            {
                while(str_p1!='\0')   //substr[] lies somewhere in the main string(in between)
                {
                    *str=*str_p1;     /*Process of replacing characters of substr[] in main str[] with characters beyond the substr[] part in the main str[]*/
                    str++;
                    str_p1++;
                }
                while(str!='\0')         //what if during this process '\0' comes up,replace rest of them with '\0'
                {
                    *str='\0';
                    str++;
                }
                return 1;         //Denotes success of finding substr[] in str[]
            }
        }
        str++;                  //Ensures that every character is tested.
        str_p1=str;
        substr_p1=substr;
    }
}


Can somebody explain this behavior of this program?

This post has been edited by JackOfAllTrades: 13 May 2013 - 03:00 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Delete a substring from a string(custom version) not working.

#2 pulp_fiction  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-February 13

Re: Delete a substring from a string(custom version) not working.

Posted 12 May 2013 - 10:43 PM

Oops,seems like I forgot to put the code code brackets,sorry for that.
Please read the re version of this post(I have posted it again)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Delete a substring from a string(custom version) not working.

Posted 13 May 2013 - 03:02 AM

Outside of NOT USING gets(), I would suggest you get acquainted with your debugger. That's the BEST way to learn what exactly is going on and will make you a better programmer.

Compile in debug mode.
Set a breakpoint in your function.
Start the program.
Step through your function and watch what's happening to the variables as you manipulate your pointers.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Delete a substring from a string(custom version) not working.

Posted 13 May 2013 - 02:48 PM

 while(str!='\0')               //Till the end of main string(its every character)



This compares the target address (not the target character) to NULL. You need to deference you pointer and compare to NULL instead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1