Write an algorithm and draw flowchart to find whether a given string S1 is substring of another string S2. 10m Dec2007
Algorithm
Step 1: Input String and Substring (Note: Substring should be smaller than String)
Step 2: Find Length of String (Len1) and Find Length of Substring (Len2)
Step 3: flag <- 0
Step 4: for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
Step 5: if (flag==1)
write(“SEARCH SUCCESSFUL!”);
else
write “SEARCH UNSUCCESSFUL!”
Step 6: Stop
FlowChart:
Program Search S:-
#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
puts(“Enter a string:”);
gets(str);
puts(“Enter search substring:”);
gets(search);
while (str[count1]!=’\0′)
count1++;
while (search[count2]!=’\0′)
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“SEARCH SUCCESSFUL!”);
else
puts(“SEARCH UNSUCCESSFUL!”);
getch();
}
[codesyntax lang=”c”]
#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
puts(“Enter a string:”);
gets(str);
puts(“Enter search substring:”);
gets(search);
while (str[count1]!=’\0′)
count1++;
while (search[count2]!=’\0′)
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“SEARCH SUCCESSFUL!”);
else
puts(“SEARCH UNSUCCESSFUL!”);
getch();
}
[/codesyntax]
Screen Shots: