Maximum Repeating Substring
Maximum Repeating Substring
Exp. No : 2.1
Date : Maximum Repeating Substring
AIM:
To write a C-program that finds the Maximum Repeating Substring .
PSEUDOCODE:
createPrefixTab(char patt[])
BEGIN
int i=0,j=1;
prefix[0]=0;
for(j=1;j<strlen(patt);j++){
while((i>0) && patt[i]!=patt[j])
i=prefix[i-1];
if(patt[i] == patt[j])
i=i+1;
prefix[j]=i;
}
END
knp(char txt[],char patt[])
BEGIN
int i=0,j=0,cnt=0;
for(j=0;j<strlen(txt);j++){
while((i>0) && patt[i]!=txt[j] )
i = prefix[i-1];
if(patt[i] == txt[j])
i=i+1;
if(strlen(patt) == i)
{
cnt++;
i=0;
}
}
return cnt;
END
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int prefix[100]={0};
void createPrefixTab(char patt[]){
int i=0,j=1;
prefix[0]=0;
for(j=1;j<strlen(patt);j++){
while((i>0) && patt[i]!=patt[j])
i=prefix[i-1];
if(patt[i] == patt[j])
i=i+1;
prefix[j]=i;
}
9 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
}
int knp(char txt[],char patt[]){
int i=0,j=0,cnt=0;
for(j=0;j<strlen(txt);j++){
while((i>0) && patt[i]!=txt[j] )
i = prefix[i-1];
if(patt[i] == txt[j])
i=i+1;
if(strlen(patt) == i)
{
cnt++;
i=0;
}
}
return cnt;
}
int main(){
char txt[100],patt[40];
printf("Enter a text string:");
scanf("%s",&txt);
printf("Enter a pattern string:");
scanf("%s",&patt);
createPrefixTab(patt);
int val=knp(txt,patt);
if(val==0)
printf("Match not found");
else{
printf("No of times pattern occured = %d",val);
}
return 0;
}
OUTPUT:
RESULT:
Thus the program for finding the no of times substring occurred in a given string was successfully
executed and the output is verified.
10 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
Exp. No : 2.2
Date : String Matching in an Array
AIM:
To write a C-program to perform String Matching in an Array for the given array of string words.
PSEUDOCODE:
createPrefixTab(char patt[])
BEGIN
int i=0,j=1;
prefix[0]=0;
for(j=1;j<strlen(patt);j++){
while((i>0) && patt[i]!=patt[j])
i=prefix[i-1];
if(patt[i] == patt[j])
i=i+1;
prefix[j]=i;
}
END
knp(char txt[],char patt[])
BEGIN
int i=0,j=0,cnt=0;
for(j=0;j<strlen(txt);j++){
while((i>0) && patt[i]!=txt[j] )
i = prefix[i-1];
if(patt[i] == txt[j])
i=i+1;
if(strlen(patt) == i)
{
cnt++;
i=0;
}
}
return cnt;
END
for(int i=0;i<4;i++)
BEGIN
createPrefixTab(words[i]);
for(int j=0;(j<4);j++)
BEGIN
if(j!=i){
int val = knp(words[j],words[i]);
if(val){
print ("%s\n",words[i]);
}
END
}
prefix[100]={0};
END
11 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int prefix[100]={0};
void createPrefixTab(char patt[]){
int i=0,j=1;
prefix[0]=0;
for(j=1;j<strlen(patt);j++){
while((i>0) && patt[i]!=patt[j])
i=prefix[i-1];
if(patt[i] == patt[j])
i=i+1;
prefix[j]=i;
}
}
int knp(char txt[],char patt[]){
int i=0,j=0,cnt=0;
for(j=0;j<strlen(txt);j++){
while((i>0) && patt[i]!=txt[j] )
i = prefix[i-1];
if(patt[i] == txt[j])
i=i+1;
if(strlen(patt) == i)
{
return 1;
}
}
return 0;
}
int main(){
char words[4][100];
printf("Enter string of Arrays");
for(int i=0;i<4;i++){
scanf("%s",&words[i]);
}
printf("Substrings are:");
for(int i=0;i<4;i++){
createPrefixTab(words[i]);
for(int j=0;(j<4);j++){
if(j!=i){
int val = knp(words[j],words[i]);
if(val){
printf("\t%s",words[i]);
}
}
}
prefix[100]={0};
}
return 0;
}
12 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
OUTPUT:
RESULT:
Thus the program to perform String Matching in an Array for the given array of string words was
successfully executed and the output is verified.
13 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
Exp. No : 2.3
Date : Sort an Array
AIM:
To write a C-program to sort an array without using any built-in functions in O(nlog(n)) time
complexity and with the smallest space complexity possible.
PSEUDOCODE:
Function Quicksort(int arr[],int low,int high)
BEGIN
if(low<high){
int pivot=low;
int i = low + 1,j = high;
while(i<=j) {
while(i<=high && arr[i]<=arr[pivot])
i=i+1
while( arr[j]>arr[pivot])
j=j=j-1
if(i<j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
int temp=arr[j];
arr[j]=arr[pivot];
arr[pivot]=temp;
Quicksort(arr,low,j-1);
Quicksort(arr,j+1,high);
}
END
SOURCE CODE:
#include<stdio.h>
int arr[30];
void Quicksort(int arr[],int low,int high){
if(low<high){
int pivot=low;
int i=low+1,j=high;
while(i<=j){
while(i<=high && arr[i]<=arr[pivot])
i++;
while( arr[j]>arr[pivot])
j--;
if(i<j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
14 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
int temp=arr[j];
arr[j]=arr[pivot];
arr[pivot]=temp;
Quicksort(arr,low,j-1);
Quicksort(arr,j+1,high);
}
}
int main(){
int n;
printf("Enter Size of array:");
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
Quicksort(arr,0,(n-1));
printf("Sorted array:");
for(int i=0;i<n;i++)
printf("%d\t",arr[i]);
}
OUTPUT:
RESULT:
Thus the C-program to sort an array without using any built-in functions in O(nlog(n)) time
complexity was successfully executed and the output is verified.
15 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
Exp. No : 2.4
Date : Median of Two Sorted Arrays
AIM:
To write a C-program for finding Median of Two Sorted Arrays.
PSEUDOCODE:
SOURCE CODE:
#include<stdio.h>
float median(int arr1[],int arr2[],int n1,int n2){
int arr[n1+n2];
int k=-1,i=0,j=0;
float res;
16 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
if((n1+n2)%2==0){
int mid=(n1+n2)/2;
res=(arr[mid]+arr[--mid])/2.0;
}
else{
int mid=(n1+n2)/2;
res=arr[mid];
}
return res;
}
int main()
{
int arr1[]={1,2};
int arr2[]={3,4};
int n1=sizeof(arr1)/sizeof(arr1[0]);
int n2=sizeof(arr2)/sizeof(arr2[0]);
printf("ARRAY 1:\t");
for(int i=0;i<n1;i++)
printf("%d\t",arr1[i]);
printf("\nARRAY 2:\t");
for(int i=0;i<n2;i++)
printf("%d\t",arr2[i]);
17 717823P202
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR304 – DATA AND ANALYSIS OF ALGORITHM
OUTPUT:
RESULT:
Thus the program for finding median of two sorted array was successfully executed and the output is
verified.
18 717823P202