CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@samaira • 12 Jul, 2009

How to write a program in c to find a substring? (without using library functions) ?

@reachrkata • 12 Jul, 2009 Using For Loops and Character comparison.

-Karthik
😁
@samaira • 13 Jul, 2009 that's quite obvious but i m not able to get the output.To be frank i want the code of the program.Plz help if u can
@Saandeep Sreerambatla • 13 Jul, 2009 Please post clear question, post what you have tried so far such taht we can guide you..
@samaira • 13 Jul, 2009 #include<stdio.h>
#include<conio.h>
int main()
{
int i=0,j=0;
char a[10],b[10];
clrscr();
printf("enter the main string: ");
scanf("%s",a);
printf("\n enter the second string: ");
scanf("%s",b);
while(a!='\0')
{
if(a==b[j])
{
i++;
j++;}

else
{
j=0;
if(a==b[j])
j++;
i++;
}
else
{
printf("the given string is not substring");
}
printf("\n the given substring is present");
}
getch();
}
just check the program and tell me where i m going wrong
@Saandeep Sreerambatla • 13 Jul, 2009 This is my understanding..

Before the first if loop you have initialized i=0 and j=0.

If that condition fails again you have initialized j=0.

So if the first IF loop fails ..
Then the If loop in the else part will also fail for j=0.

This functionality is very easy by using for loops.

First take the main string into a[] , then substring into b[]

Use two for loops and you will get the answer.
@samaira • 13 Jul, 2009 i have initialized j=0 again after the if loop fails coz the string might appear at any position of the second string like e.g we have to find whether the string "comp" is a substring of "mech & comp".so after the condition fails the second string is again ready to be checked from first while the counter of second goes on increasing.
@shalini_goel14 • 13 Jul, 2009 Hi samaira,

int blen=0, k=0;

//assign length of be to blen
blen =strlen(b); //not sure

 while(a[i]!='\0')
      {
       if(a[i]==b[j])
          {
           i++;
           j++;
if(j==blen){
j=0;
}

}

        else
        {
            j=0;
          //if(a[i]==b[j])
           //j++;
        i++; 
           }
        else
{
        printf("the given string is not substring");
}
printf("\n the given substring is present");
}
getch();
}
Try this including b length's check in your program, if it works. Also, I think you can include a separate variable k in your program and initialize it to 0 and set its value as 1 when your check in if loop works and then at the end while printing message you can check if it is 1 then "matched" else "unmatched"

Thanks !
@samaira • 13 Jul, 2009 hey shalini thanks for ur help.:smile:
@shalini_goel14 • 13 Jul, 2009 @Samaira If you program works then don't forget to post the complete working program here also.

Thanks !
@yogesh_dhiman • 23 Jul, 2009 //Without using Functions.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(string.h)
void main( )
{
char st[20],sub[10],temp[10];
int pos, i, j;
clrscr( );
printf("Enter the main string:");
gets(st);
printf("\nEnter the substring to insert:");
gets(sub);
printf("Enter the index position:");
scanf("%d",&pos);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st!='\0';j++) // to store the 'st' to 'temp' from given position.
{
temp[j]=st;
i++;
}
temp[j]='\0';
i=pos;
for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.
{
st=sub[j];
i++;
}
for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.
{
st=temp[j];
i++;
}
st='\0';
}}
printf("\nAfter adding the sub-string: %s",st);
}
else
printf("\nSorry, it is not possible to insert a substring in that position.");
getch();
}
@jkr0103 • 05 Jul, 2015
[HASHTAG]#include[/HASHTAG] <iostream>
using namespace std;
int main()
{
    char a[] = {"Hanuman"};
    char b[] = {"Hanuu"};
    int i = 0, j = 0;
    while(a[I]!='\0')
    {
        if(a[I]==b[j])
        {
            j++;
            if (b[j] == '\0')
                break;
        }
        else
        {
            i = i-j;
            j = 0;
        }
        i++;
    }
    if(b[j] == '\0')
    {
        cout << "substring exists\n";
    }
    else
    {
        cout << "substring does not exists\n";
    }
    return 0;
} 
[/I][/I]
11.2k views

Related Posts

@Guneet Khanuja · Aug 6, 2014

Appointy, an online scheduling software, was founded by Nemesh Singh in 2006. Appointy is an advanced web-based scheduling software tool that works on .NET & MSSql. Appointy allows users to...
3.6k views

@Ankita Katdare · Jun 5, 2014

Moving beyond passwords for authentication when logging in to a system, an app, an online service was a long-time calling. Largely because of the increase in incidents of passwords getting...
2.5k views

@Guneet Khanuja · Jun 9, 2014

Witworks is a Bangalore-based collaborative invention company founded by Somnath Meher, Ankit DP and Chandrashekhar Iyer. Incubated and funded by the Indian Institute of Technology, Kharagpur, Witworks develops and sells...
2.4k views

@Ankita Katdare · Feb 23, 2014

A lot of engineers among us might be trying to go for MBA after their graduation in B.E. or B.Tech.If you face a personal interview for any MBA college, you...
99.9k views

@me@ce · May 13, 2011

Hi to all CE members! In coming month I am planning to buy a new TV screen, I think LG is the best brand, but one of my cousins was...
15.6k views