0% found this document useful (0 votes)
5 views

Assignment 1

Uploaded by

hamajis508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 1

Uploaded by

hamajis508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write a program to search a word in a sentence using


(a) Array

#include <stdio.h>
#include <string.h>
int main()
{
char str1[100],str2[100];
int i=0,j;

printf("Enter the sentence\n");


scanf("%[^\n]",str1);
printf("Enter the word that you want to find\n");
scanf("%s",str2);
int n= strlen(str1);
int m=strlen(str2);
while(i<n)
{
int count=0;
int j=0;
while(i<n&&j<m&&str1[i]==str2[j])
{
i++;
j++;
count++;
}
i++;
if(count==m)
{
printf("YES, the word is in the sentence\n");
return 0;
}

}
printf("NO, the word is not in sentence\n");
return 0;
}

(b) Pointer

#include <stdio.h>
#include<string.h>
int funct(char *str1,char *str2, int n, int m)
{
int i=0,j,k=0;
{
while(i<n)
{
int count=0;
j=0;
while(i<n&&j<m&&str1[i]==str2[j])
{
k=i;
i++;
j++;
count++;
}
if(count==m)
{
printf("YES, the word is in the sentence\n");
return 0;
}
i++;

}
printf("NO, the word is not in sentence\n");
return 0;
}
}
int main()
{
char str1[100],str2[100];
int i=0,j;

printf("Enter the sentence\n");


scanf("%[^\n]",str1);
printf("Enter the word that you want to find\n");
scanf("%s",str2);
int n= strlen(str1);
int m=strlen(str2);
funct(str1,str2,n,m);
return 0;
}
2. Implement insertion sort using pointer mention any real-world
application of insertion sort in your program

#include <stdio.h>

void print(int *num, int n) {


int i;
for(i = 0; i<n; i++) {
printf("%d ", *num);
num++;
}
printf("\n");
}

void shift(int *arr, int *i) {


int value;
for (value = *i; i > arr && *(i-1) > value; i--) {
*i = *(i-1);
}
*i = value;
}

void insertion(int *arr, int len) {


int *i, *last = arr + len;
for(i = arr + 1; i < last; i++)
if(*i < *(i-1))
shift(arr, i);
}

int main() {

int arr[5] = {10,15,12,18,1};


int n = sizeof(arr)/sizeof(int);

print(&arr[0], n);

insertion(&arr[0], n);

print(&arr[0], n);

APPLICATION

1. If you know your lists are never going to contain more than say 25 elements, then
insertion sort is an excellent choice. It is very simple and it’s going to be more
efficient than any more complicated sort like quick-sort.
2. great for small or mostly-sorted arrays
3. Real time application

You have a shopping list, and your wife is telling you to grab them in 15 minutes.
She gives you also priorities, so you need to grab them first. You gotta rush!

Shopping list (Item/Priority);

1. Eggs (4)
2. Bread (2)
3. Milk (6)
4. Water (3)
5. Meat (1)
6. Detergent (5)

You might also like