Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
just doesn't get right output ..
the logic is right but can't find the bug.
if i enter any string and substring,the output is just one
Substring not found..
plz help me

What I have tried:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
    int i,j,pos,flag;
    char sub[100],main[100];
    cout<<"enter main string"<<endl;
    gets(main);
    cout<<"enter substring"<<endl;
    gets(sub);
    for(i=0;main[i]!='\0';++i)
    {
        if(main[i]==sub[0])
        {
            pos=i,j=0;
            while(sub[j]==main[i])
            {
                i++ ;
                j++;
            }
        }
        if(j==strlen(sub))
            flag=1;
        else
            flag=0;
    }
    if(flag== 1)
        cout<<"substring found at"<<pos+1<<endl;
    if(flag==0)
        cout<<"substring not found"  ;
    getch();
}
Posted
Updated 9-Jun-16 0:23am
v2

Your program is working mainly working as expected but not when the substring is at the end of the main string because you are not checking for the end of the strings. The terminating null characters will match too so that j becomes greater than the length of the sub string.

There is also another error which should be shown during compilation or during run-time with debug builds: The variable j may be used without initialisation. This can be avoided by placing the code that sets the flag into the above if condition.

Finally you should restore i when a charcter match has been found. This avoids also an overrun when matching at the end of main (then i points to the terminating null character, is incremented at the end of the loop and the following null check of the loop condition might fail).

The updated code part:
if(main[i]==sub[0])
{
    pos=i,j=0;
    // Check for end of string too
    while(sub[j]==main[i] && main[i] != '\0')
    {
        i++;
        j++;
    }
    // Moved this into if condition
    if(j==strlen(sub))
        flag=1;
    else
        flag=0;
    // Restore i here
    i = pos;
}
 
Share this answer
 
Comments
[no name] 9-Jun-16 11:46am    
bro i=pos is not required.. as 'i' is a local variable inside while loop
the 1st suggestion is correct
thnxx for help
Jochen Arndt 9-Jun-16 12:02pm    
It is is required for the above code when not breaking at the first match (as suggested in the other solution) and the sub string is at the end of the string.

Then i is the index of the terminating NULL character, incremented at the end of the loop outer loop and points behind the string (out of array access). If the character at that position is not NULL (which is probable), the loop continues behind the string and stops at the next random NULL byte. As a result your print statement will show a wrong position.

It must be also set there if you want to detect multiple matches witch can be indicated by moving the found match print to there where flag is set.
I assume you are doing this as exercise (otherwise your should use string::find, see string::find - C++ Reference[^]).

You have to exit the foor loop on successful match, that is change from
Quote:
if(j==strlen(sub))
flag=1;
to
C++
if(j==strlen(sub))
   {
       flag=1;
       break;
   }


You also miss end-of -tring checking in your code, chage from
Quote:
while(sub[j]==main[i])
to
C++
while(main[i] != 0 && sub[j]==main[i])



Note, also, you are using an outdated compiler and deprecated functions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900